Redirecting old Query String URLs to CMSMS pretty urls

Do something cool with CMS? Show us ...
This board is for 'Answers', and the discussion of answers... Not for questions.
Post Reply
steffeiter
Forum Members
Forum Members
Posts: 13
Joined: Fri Dec 17, 2010 1:08 am

Redirecting old Query String URLs to CMSMS pretty urls

Post by steffeiter »

All -

This took me about an hour and a half to figure out, so I wanted to post for other users who use CMSMS and are coming from old php sites.

Pretty URLs (mod_rewrite) does not correctly interpret 301 redirects in the .htaccess file when the original url has a query string in it: index.php?someID=someVal

So the following works to make the .htaccess mod_rewrite work correctly:

Below the rewrite engine rule is where to edit:

Code: Select all

Options +FollowSymLinks
RewriteEngine on

# Rewrite all the old php pages to the new CMS pages
# by adding lines here...

# 301 Redirect all requests that don't contain a dot or trailing slash to
# include a trailing slash
I used the following directionals based on an OLD url like this:

Code: Select all

http://www.example.com/originalPage.php?someID=someVal

Code: Select all

RewriteCond %{QUERY_STRING} ^.*someID=someVal.*$
RewriteRule ^originalPage http://www.example.com/the/pretty/url/path/to/your/new/page/? [R=301,L]
Notes:
The RewriteCond has the key-value pair that you have used in your old URLs. here it is "someID" and "someVal"

The RewriteRule has two important things. ^originalPage (this is the original page name from your site WITHOUT the extension .php or whatever)
the second important thing is to add the "?" after the last / - this removes the original query string from the new rewritten url.


I know this isn't totally a CMSMS issue, but this explanation is specifically for CMSMS so please leave this post.
It took me a while to figure this out.
If anyone has other things to add, please do....

-steff
kendo451

Re: Redirecting old Query String URLs to CMSMS pretty urls

Post by kendo451 »

It's a lot easier to just put a canonical tag in your template.

See this entry:
http://forum.cmsmadesimple.org/viewtopic.php?t=37333

This post shows how to create a UDT to make a canonical link:
http://forum.cmsmadesimple.org/viewtopic.php?t=30923
steffeiter
Forum Members
Forum Members
Posts: 13
Joined: Fri Dec 17, 2010 1:08 am

Re: Redirecting old Query String URLs to CMSMS pretty urls

Post by steffeiter »

Hi -

I am not sure that canonical urls are at issue here.
I am not rerouting previous or current cmsms pages.
I am rerouting old php pages from a former site that
have been cached by google but do not exist anymore.

How would a canonical url be used for page that doesn't exist?

-steff
kendo451

Re: Redirecting old Query String URLs to CMSMS pretty urls

Post by kendo451 »

You are quite right, Steff. I misunderstood and thought you were attempting to create a redirect table for the regular CMSMS index.php?page=alias .

Of course the standard mod_rewrite .htaccess file does that for you already.
Skulks
Forum Members
Forum Members
Posts: 11
Joined: Wed Jul 02, 2008 1:40 pm

Re: Redirecting old Query String URLs to CMSMS pretty urls

Post by Skulks »

I need to 301 my old url's

Code: Select all

http://www.torremolinos-nights.com/index.php?page=doyle-s-corner
To the new ones

Code: Select all

http://www.torremolinos-nights.com/bars/doyle-s-corner.html
Your instruction say use ? after the last slash but my address ends in doyle-s-corner.html

My htaccess looked like this

Code: Select all

# Attempt to override some php settings, these settings may be helpful on some hosts if your
# default configuration does not meet CMS's minimum requirements, and your host
# has given your account appropriate permissions
#php_value upload_max_filesize "10M"
#php_value session_save_path "tmp/cache"

#php_flag magic_quotes_gpc Off
#php_flag register_globals Off
#php_flag session.use_trans_sid Off

# (this is important, so uncomment if your host permit)
#Options -Indexes
#ServerSignature Off
#
Options +FollowSymLinks
#
<IfModule mod_rewrite.c>
RewriteEngine on
#
#Sub-dir e.g: /cmsms
RewriteBase /

#
# 301 Redirect all requests that don't contain a dot or trailing slash to
# include a trailing slash
# but ignore POST requests.
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.
RewriteCond %{REQUEST_METHOD} !POST$
RewriteRule ^(.*) %{REQUEST_URI}/ [R=301,L]

# 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]
</IfModule>

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{QUERY_STRING} ^.*page=doyle-s-corner.*$
RewriteRule ^index http://www.torremolinos-nights.com/bars/doyle-s-corner.html [R=301,L]
Can anyone tell me whats wrong or give me a single example, please. I'm a bit scared to just carry on fiddling with the htaccess
steffeiter
Forum Members
Forum Members
Posts: 13
Joined: Fri Dec 17, 2010 1:08 am

Re: Redirecting old Query String URLs to CMSMS pretty urls

Post by steffeiter »

jackherer wrote:
...so I couldn't use your code as is and I had to try a load of different things but after a while I seem to have worked it out:

Code: Select all

RewriteCond %{QUERY_STRING} ^.*page=foo.*$
RewriteRule ^(.*)$ http://www.example.com/the/pretty/url/path/to/your/new/page/? [R=301,L]
Thanks again, I wouldn't have know where to start so you saved me a lot of time!
I am pretty sure, though haven't tried it, but couldn't you have written:

Code: Select all

RewriteRule ^index ....

as by default the index page is what is being routed through?
steffeiter
Forum Members
Forum Members
Posts: 13
Joined: Fri Dec 17, 2010 1:08 am

Re: Redirecting old Query String URLs to CMSMS pretty urls

Post by steffeiter »

@skulks

So I am looking at your code and tried to replicate on my server, and I am able to do what you need.

Code: Select all

RewriteCond %{QUERY_STRING} ^.*page=list.*$
RewriteRule ^index http://localhost/cmsmadesimple-1.9.4.2/uploads/index.html? [R=301,L]
I added the ? just after the html. It shouldn't affect anything, as the trailing ? is there just to remove the query string form the original url.

This code redirects this page:
/index.php?page=list

to

/uploads/index.html


I thought maybe it was because you have written "RewriteEngine on" twice in your .htaccess file.

Or maybe your server doesn't like that you are writing you rewrites outside of the <IfModule mod_rewrite.c></IfModule>

didn't seem to affect mine, but maybe?


-steff
Post Reply

Return to “Tips and Tricks”