[SOLVED]301 redirect still accessible query string urls

The place to talk about things that are related to CMS Made simple, but don't fit anywhere else.
Post Reply
userwords
Forum Members
Forum Members
Posts: 48
Joined: Mon Dec 27, 2010 12:27 am

[SOLVED]301 redirect still accessible query string urls

Post by userwords »

Unless im wrong this line with a query string append

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]

in official htaccess make pretty url, like:

http://www.mydomain/title

Ive found that non pretty url are still accessible:

http://www.mydomain/index.php?page=title

What could i add to make them 301 redirect to pretty url.

Ive tried adding a 301 redirect flag like

RewriteRule ^(.+)$ index.php?page=$1 [QSA, r=301]

Which breaks the whole web ::)

And adding a rewrite after that one like this

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]

RewriteRule ^/index.php?page=$ /$1 [R=301,NC,L]

That im sure you can tell from reading that line, i dont understand how to properly lay a rewrite.

Ive read a lot, but is like trying to know about something that is in the middle of a lot of other server concepts that are beyond my scope and knowledge.


As i understand that would be something useful for anyone, could someone come with a proper rewrite to add after the oficial one, or a new version of the official one, that would redirect 301, those unused urls.
Last edited by userwords on Tue Oct 29, 2013 11:03 pm, edited 1 time in total.
User avatar
Rolf
Power Poster
Power Poster
Posts: 7825
Joined: Wed Apr 23, 2008 7:53 am
Contact:

Re: How to 301 redirect still accessible query string urls

Post by Rolf »

This is something what the canonical url is for...
- + - + - + - + - + - + -
LATEST TUTORIAL AT CMS CAN BE SIMPLE:
Migrating Company Directory module to LISE
- + - + - + - + - + - + -
Image
userwords
Forum Members
Forum Members
Posts: 48
Joined: Mon Dec 27, 2010 12:27 am

Re: How to 301 redirect still accessible query string urls

Post by userwords »

Oh no, i mean, i know this wont be fixing any issue, as you say there is already a canonical to tell engines what url to assign the page. Maybe is more on the usability side, and frankly in the paranoid side :D
User avatar
velden
Dev Team Member
Dev Team Member
Posts: 3497
Joined: Mon Nov 28, 2011 9:29 am

Re: How to 301 redirect still accessible query string urls

Post by velden »

Rolf wrote:This is something what the canonical url is for...
I don't think a canonical url will solve this 'problem'. However it should prevent search engines to index the query string urls.

That's probably what you want?

You probably don't want to 'break' the query string urls. You might get into trouble (eg. some modules might not output pretty urls for a reason).
userwords
Forum Members
Forum Members
Posts: 48
Joined: Mon Dec 27, 2010 12:27 am

Re: How to 301 redirect still accessible query string urls

Post by userwords »

velden wrote:
Rolf wrote:This is something what the canonical url is for...
I don't think a canonical url will solve this 'problem'. However it should prevent search engines to index the query string urls.

That's probably what you want?

You probably don't want to 'break' the query string urls. You might get into trouble (eg. some modules might not output pretty urls for a reason).
My intention was to redirect 301 the non pretty url to the pretty version. Actually i thought that was already done with that [qsa] line, but i see it makes the pretty url, but leave the page accessible under the previous one.

I dont think this may be of concern, although i have doubts if someone could exploit it maliciously making links to the other version. But again i suppose the canonical tell the engine what to use so there should be no issues.

Again, this is paranoid, or picky. I simply did suppose it was already redirected, and ask myself if something could be added to make it happen.
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: How to 301 redirect still accessible query string urls

Post by calguy1000 »

I don't think you understand how URLS work.

A: CMSMS generates URLS based on config settings.
those URLS are clicked on, shared, whatever...

B: The apache server receives an incoming request
it passes the incoming request through mod_rewrite for any matching rules
mod_rewrite may transform the incoming URL into a different form

The default htaccess file that ships with CMSMS explicitly says:

Code: Select all

# Rewrites urls in the form of /parent/child/
# but only rewrites if the requested URL is not a file or directory
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?page=$1 [QSA]
C: The url, which may have been rewritten by mod_rewrite is processed.normally by apache... and probably the CMSMS php script is executed.

CMSMS requires the index.php... unfriendly syntax. mod_rewrite just rewrites incoming requests so that they are in that format.

CMSMS accepts URLS in a number of formats (I've explained it numerous times before). This URL flexibility is not a flaw, security vulnerability, or weakness in any way. It is a feature.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
userwords
Forum Members
Forum Members
Posts: 48
Joined: Mon Dec 27, 2010 12:27 am

Re: How to 301 redirect still accessible query string urls

Post by userwords »

Im impressed with such a detailed explanation. Read through it carefully. And agree that by no means i had an clear idea of the process url follows and how server and cmsms relate each other. I however know that cmsms use internally that url, and again agree you have said it hundred times, because ive actually learned that from some of your post :D.

This rewrites are some kind of a mask facing the user that has nothing to do with what cmsms uses internally. What i had curiosity about is if that url with the query string, that i know is the real one, and will always be there, can be externally redirected 301 to the canonical one. So if someone tries to access http://www.mydomain.com/index.php?page=title

content loads, because the url is not only valid, it is actually the real one, but browser rewrites the url field in the window because of the redirection to

http://www.mydomain.com/title
User avatar
Rolf
Power Poster
Power Poster
Posts: 7825
Joined: Wed Apr 23, 2008 7:53 am
Contact:

Re: How to 301 redirect still accessible query string urls

Post by Rolf »

velden wrote:
Rolf wrote:This is something what the canonical url is for...
I don't think a canonical url will solve this 'problem'. However it should prevent search engines to index the query string urls.
It *will* solve the "problem" because no one knows the index.php?page=foo url!
In none of my websites the ugly url is used and appears in the analytics.
- + - + - + - + - + - + -
LATEST TUTORIAL AT CMS CAN BE SIMPLE:
Migrating Company Directory module to LISE
- + - + - + - + - + - + -
Image
calguy1000
Support Guru
Support Guru
Posts: 8169
Joined: Tue Oct 19, 2004 6:44 pm

Re: How to 301 redirect still accessible query string urls

Post by calguy1000 »

The rewrite rule specified above translates urls in the form of

http://mysite.com/something/somethingelse/alias

and does a redirect to:

http://mysite.com/index.php?page=someth ... else/alias

That string (something/something/alias) is then matched through all of the registered 'routes'. and also matched against page aliases.

Therefore the 'redirect' is from the pretty url something/somethingelse/alias to index.php?page=something/somethingelse/alias

If there was no redirect, apache would generate 404 errors... because obviously there is no file something/somethingelse/alias whereas there is an index.php file in the CMSMS root directory. and apache knows to execute .php files with the php executable.
Follow me on twitter
Please post system information from "Extensions >> System Information" (there is a bbcode option) on all posts asking for assistance.
--------------------
If you can't bother explaining your problem well, you shouldn't expect much in the way of assistance.
userwords
Forum Members
Forum Members
Posts: 48
Joined: Mon Dec 27, 2010 12:27 am

Re: How to 301 redirect still accessible query string urls

Post by userwords »

calguy1000 wrote:The rewrite rule specified above translates urls in the form of

http://mysite.com/something/somethingelse/alias

and does a redirect to:

http://mysite.com/index.php?page=someth ... else/alias

That string (something/something/alias) is then matched through all of the registered 'routes'. and also matched against page aliases.

Therefore the 'redirect' is from the pretty url something/somethingelse/alias to index.php?page=something/somethingelse/alias

If there was no redirect, apache would generate 404 errors... because obviously there is no file something/somethingelse/alias whereas there is an index.php file in the CMSMS root directory. and apache knows to execute .php files with the php executable.
I was looking at the redirect from the opposite side i should, in terms of url rewritten and served, instead of a translator that turn incoming url petitions to the real ones of cmsms. And i see as suggested that there is not any issue with it. There are very good explanations here. I appreciate very much your dedication.
Post Reply

Return to “The Lounge”