Forum Moderators: phranque
A) To change a small number of dynamic urls to more 'friendly' urls.
e.g.change
http://www.example.com/index2.php?content_id7
to
http://www.example.com/faq.htm
This is achieved by:
1. Changing internal links to new faq.htm
2. Creating the following internal rewrite line in .htaccess:
RewriteRule faq\.htm$ index2.php?content_id=7 [L] Which works very well for my needs.
(Note: External back-links are currently are not an issue with this site - they all point to http://www.example.com)
B) To update SE indexes by removing old dynamic urls from index and pointing to new url permanently:
e.g.
...index2.php?content_id7 with ...new faq.htm This is (theoretically) to be achieved by:
Create an External redirect using .htaccess: I have tried this many different ways but it always gives an error 500. Even trying a simple one like this does not work:
e.g. RedirectMatch 301 login.php login.htm
So my .htaccess might look like this:
RewriteEngine on
RewriteBase /
# internal redirect - works fine
RewriteRule login\.htm$ login.php [L]
#External SEO redirect - returns error 500
RedirectMatch 301 login.php login.htm
this also does not work:
#RewriteRule index2\.php?content_id=7$ faq.htm [R=301, L]
I have tried using redirect, redirectmatch and rewrite to achieve this but it won't play.
Any chance of some suggestions as what I am doing wrong?!..you'd save my bacon as I am delaying putting this site live due to this!
[edited by: jdMorgan at 12:29 pm (utc) on Oct. 25, 2007]
[edit reason] example.com [/edit]
In order to determine why this causes a server error, it's necessary to know what the relationship between the two URL-paths is. For example, are both defined as DirectoryIndex pages? Is login.htm later rewritten to login.php, creating an 'infinite' rewrite/redirect loop?
Also, it would be most helpful to know why the server is generating a 500-Server Error -- This information is available in your server error log.
It's not trivial to do what you're probably trying to do and avoid looping, even with a well-defined scope. For example, in order to rewrite from "/" to "index.php" and to redirect client requests from index.php to "/" you'd need:
RewriteRule ^$ /index.php [L]
#
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/ [R=301,L]
Jim
RewriteRule login\.htm$ login.php [L]RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /login\.php\ HTTP/
RewriteRule ^login\.php$ http://example.com/login.htm [R=301,L]
Which works!
I had a hunch looping was the issue but couldn't see the wood for the trees. I was close at times but using the HTTP part of the request is an eye opener. Thank you so much.
...and thank you for the forum year after year - excellent stuff.
I got the example (above) working but seem to be having trouble with the dynamic url - Is there something I need to escape that I'm missing?
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?content_id=1\ HTTP/
RewriteRule ^index\.php\?content_id=1$ http://example.com/home.htm [R=301,L]
I have tried without escaping the '?' and/or with escape for t he underscore but no joy - it does not pick it up.
Any thoughts?
I am not totally clear on this part of the expression you provided:
^[A-Z]{3,9}\
Is it something do with this not picking up the '?' and the '_'? Would you mind explaining this part for clarity.
Thank you again.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?content_id=1\ HTTP/
RewriteRule ^index\.[b]php$[/b] http://example.com/home.htm [R=301,L]
GET /index.php?content_id=1 HTTP/1.1
Jim
# index.php?content_id=1
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?content_id=1\ HTTP/
RewriteRule ^index\.php$ http://example.com/home.htm [R=301,L]
SHOULD 'replace' example.com/index.php?content_id=1
with example.com/home.htm
BUT in fact it 'replaces and appends' with following result:
example.com/home.htm/index.php?content_id=1
Why is it appending the original index.php?content_id=1?
How do you know this? Is there a good resource you can recommend to lookup things like terminating the replacement string?..because I can't find it!
Can I push my luck and ask one more thing?!
I now have another similar, but different issue:
say I want to redirect (301) example.com/search.php with example.com/search.htm
BUT
not to redirect search.php if it has additional elements attached:
e.g. search.php?action=results&page=2
Would I need to write an additional rewrite condition to say
e.g. but NOT if search.php?action=results&page=2 if so can show me a not expression - I know its possible to use the <!> but I can't find the syntax for this.
....Or is there a way to say redirect if = search.php and NOT if <> search.php
Once again thank you for your excellent advice and instruction.
For more information, see the documents cited in our forum charter [webmasterworld.com] and the tutorials in the Apache forum section of the WebmasterWorld library [webmasterworld.com].
Jim