Forum Moderators: phranque
I had a URL structure like this:
http://www.example.com/modules.php?name=News&file=article&sid=8774
and I rewrote it to this:
http://www.example.com/article8773.html
using this code in my .htaccess file:
RewriteRule ^article([1-9][0-9]*).html modules.php?name=News&file=article&sid=$1
Now I need to 301 redirect all the old dynamic URLs to the new, static URLs. Can you please indicate me a solution?
Thank you.
second step, I tryed this:
RewriteRule ^article([1-9][0-9]*).html modules.php?name=News&file=article&sid=$1
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /modules\.php\?name=News&file=article&sid=([1-9][0-9]*)\ HTTP/
RewriteRule ^modules\.php$ http://www.example.com/article%1.html? [R=301,L]
but I get an error from Firefox that is telling me the page I try to access is redirecting in an "infinite loop"...
RewriteRule ^article([1-9][0-9]*).html modules.php?name=News&file=article&sid=$1 [L]
RewriteCond %{THE_REQUEST} ^[a-z](0,9)\ /modules\.php\?name=News&file=article&sid=([1-9][0-9]*)\ HTTP/
RewriteRule ^modules\.php$ http://example.com/article%1.html? [R=301,L]
.. but still don't work.
if I try to access example.com/article9999.html, it redirects me to example.com/article.html
may it be a problem that I have this code before in .htaccess ?
# code to redirect /index.php to /
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.example.com/$1 [R=301,L]
List all three of those redirects (in order from most specific, to general) before your rewrite. Some of your redirects have two lines of code (a condition and a rule) that must not be split apart.
Your index redirect is better coded without .* and as per the examples linked from the sticky thread at the top of the forum.
Split out your four bits of code (three redirects and one rewrite) with a blank line between each one. Add a # comment before each of the four blocks.
[edited by: g1smd at 10:21 pm (utc) on Dec. 26, 2008]
here is my code again, if I [haven't made you angry] :
#redirect line
RewriteRule ^modules\.php$ http://example.com/article%1.html? [R=301,L]
#rewrite line
RewriteRule ^article([1-9][0-9]*).html modules.php?name=News&file=article&sid=$1 [L]
#don't know exactly what is that line for
RewriteCond %{THE_REQUEST} ^[a-z](0,9)\ /modules\.php\?name=News&file=article&sid=([1-9][0-9]*)\ HTTP/
now, all my addresses go to example.com/article.html (no article number in URL)
thank you again for your time
Later Edit:
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.example.com/$1 [R=301,L]
RewriteRule ^modules\.php$ http://example.com/article%1.html? [R=301,L]
RewriteRule ^article([1-9][0-9]*).html modules.php?name=News&file=article&sid=$1 [L]
RewriteCond %{THE_REQUEST} ^[a-z](0,9)\ /modules\.php\?name=News&file=article&sid=([1-9][0-9]*)\ HTTP/
[edited by: jdMorgan at 10:29 pm (utc) on Dec. 26, 2008]
[edit reason] tidied-up [/edit]
- index redirect (two lines - condition and rule) - but use the example without .* from other forum examples.
- modules redirect (two lines - condition and rule) - not sure why you have split them up; they must be together.
- non-www to www redirect (two lines - condition and rule) - get this new code from other forum examples.
- rewrite (one line)
Check all the previous comments for other stuff still missing.
Add four comments (one to each block) explaining what each block does.
[edited by: g1smd at 10:32 pm (utc) on Dec. 26, 2008]
It is quite impossible to get your code to work by "guessing" and it is quite dangerous to try -- This is server configuration code and you can easily ruin your search engine rankings with a single incorrect character. Please study the documentation to save yourself a great deal of trouble.
I would suggest replacing all of your code with this:
# Externally redirect only direct client requests for /index.php to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index.php\ HTTP/
RewriteRule ^(([^/]+/)*)index.php$ http://www.example.com/$1 [R=301,L]
#
# Externally redirect only direct client requests for dynamic URL-paths to static URLs
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /modules\.php\?name=News&file=article&sid=([0-9]+)\ HTTP/
RewriteRule ^modules\.php$ http://www.example.com/article%1.html? [R=301,L]
#
# Externally redirect to canonicalize the domain name if a non-canonical
# hostname is requested, in order to prevent duplicate-content problems
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
# Internally rewrite requests for static URL-paths to dynamic filepaths
RewriteRule ^article([0-9]+)\.html modules.php?name=News&file=article&sid=$1 [L]
I have made several of the changes recommended above, as well as some other minor tweaks. All are intentional and either fix problems or improve performance.
Please do not use this code until you have read the documentation, analyzed this code with the documentation in-hand, and understand every character in every line. It is unsafe to do otherwise. It is also not a very good idea to have to depend on strangers posting in forums in order to be able to maintain your own Web site...
Jim