Forum Moderators: phranque

Message Too Old, No Replies

Mod rewrite help- modify "/" in URL to "?"

         

georgec

8:30 pm on Nov 15, 2008 (gmt 0)

10+ Year Member



Hi guys:
Just looking for some quick help on mod_rewrite. My site's URL structure recently changed slightly, from the old

[mysite.com...] (old)

to:

[mysite.com...] (current)

In other words, the character immediately following index.php went from "?" to "/". This is wrecking havoc for me with Google, as it still has thousands of pages indexed using the old URL structure that it doesn't want to give up. So basically I'd like to do a 301 Moved redirect in my .htaccess code so "index.php?" is effectively redirected to "index.php/" for all pages within /dir/. From what I gathered reading around here, this is what I have so far:

[pre]RedirectMatch 301 (index\.php\?)?$ index\.php/[pre]

I think I'm missing something with the second "index.php" above. Thanks for the help.

Thanks,

g1smd

10:00 pm on Nov 15, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you have changed to URLs that look like folders why didn't you change to something even better by ditching the /index.php/ part out of the URL, and maybe even going extensionless by dropping the .html part too?

The question you ask, or a close variation of it, has been asked at least 5 times in the last 10 days. Please check the recent threads here in the forum, and the tutorials linked from the sticky thread at the top of the forum for more examples.

I would use a RewriteRule for this, because you will need a RewriteCond to deal with the QUERY_STRING part - as that is data appended to the URL. That way you can redirect old URLs to the exact new URLs rather than fold all the old URLs into just the folder.

You'll minimally need a Rewrite (I assume you already have that bit sewn up), but you should also consider a redirect for domain canonicalisation, as well as the redirecting of requests for the old URL over to the new URL that you asked about.

Be aware that RewriteRule can be coded to make rewrites or redirects. It's like a Swiss Army Knife; multipurpose.

This thread has all the steps and some extra ones, but for a URL with two parameters: [webmasterworld.com...]

georgec

3:55 am on Nov 16, 2008 (gmt 0)

10+ Year Member



Hi:
Thanks for the reply. The structure of the new URLs aren't really up to me to decide, as it's a premade CMS script. I've gone through a lot of the threads already on mod_rewrite. Here's what I've come up with since my last code:


RewriteEngine on
RewriteRule index.php?/([^/\.]+)\.html/?$ index.php/$1.html [L]

Can you tell me if I'm missing something here? Basically I need to redirect index.php?anything.html to index.php/anything.html as a 301. Not sure where I'd place the 301 redirect in the above though.

Thanks,

jdMorgan

4:13 am on Nov 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As g1smd stated, you must use a RewriteCond to test and/or create a back-reference to a query string:

RewriteEngine on
#
RewriteCond %{QUERY_STRING} ^(.+\.html)$
RewriteRule ^index\.php$ http://www.example.com/index.php/%1 [R=301,L]

Be advised that if your server already contains code to rewrite or redirect /index.php/xyz.html to /index.php?xyz.html, then that code and the code above will interact, with each ruleset countermanding the other in an 'infinite' loop. The result will be a "Maximum redirection limit reached" 500-Server error. If this is the case, then a somewhat-more-complicated solution will be needed, involving a check of THE_REQUEST to see what the client requested before redirecting.

Jim

georgec

5:49 am on Nov 16, 2008 (gmt 0)

10+ Year Member



Hi Morgan:
Thanks for the help! I didn't realize the query string wasn't passed into RewriteRule. And there I was wondering why no pattern would match that part of the URL. :) Anyhow, I took your example and tried to modify it so the new, redirected URL no longer has the original query string (ie: ?f-41.html) tacked to the end of it. It seems adding a "?" does the trick:


RewriteCond %{QUERY_STRING} ^(.+\.html)$
RewriteRule ^index\.php$ http://www.example.com/dir/index.php/%1? [R=301,L]

Can you let me know if that's in fact correct? A quick test seems to suggest it is, but you never know with mod_rewrite I've learned.

g1smd

6:25 pm on Nov 16, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Yes, the ? on the end clears the query string.

georgec

7:22 am on Nov 17, 2008 (gmt 0)

10+ Year Member



Thanks g1smd. I'm a complete noob when it comes to mod_rewrite actually. I only figured out to adding "?" while looking at some example code.

jdMorgan

2:21 pm on Nov 17, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This function is documented in the Apache mod_rewrite documentation of RewriteRule. It's worth a read...

Jim