Forum Moderators: phranque
Situation is this. I have a simple CMS on a site that generates article pages in PHP variable format. i.e. www.example.com/article.php?id=14 I used this redirect to make the URLs nice.
Options +FollowSymLinks RewriteEngine on RewriteRule article-id-(.*)\.php article.php?id=$1 Problem is that I need to redirect the old pages (article.php?id=14)to their rewritten variants.
Can anyone help me here please?
[edit]
I did read the library thread on this and came up with this but it has not worked, I guess because I messed up somewhere.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?id=(.*)\ HTTP/ RewriteRule ^index\.php$ http://example.com/article-id-(.*) [R=301,L] [edited by: GF_Diablos at 3:08 pm (utc) on Sep. 8, 2008]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?id=([^\ ]+)\ HTTP/
RewriteRule ^index\.php$ http://example.com/article-id-%1 [R=301,L]
Jim
I am not sure exactly what is causing the problem, especially since you corrected my errors. Although I see my main problem, is I got the wild card and variable things confused.
I will try to get a bit more handle on the rewrite rule stuff as it is obviously this I don't understand but any ideas you can suggest why my site would break when trying to implement this would be great.
In general, the URL-path patterns in RewriteRules must exactly match the URL-path specified in the requested URL, but with the path to the current .htaccess file stripped. That is, if the requested url is example.com/foo/bar.html, and the rewriterule is in /foo/.htaccess, then the pattern in the rule must be "^bar\.html$" -- The rewriterule in the .htaccess file located in /foo will not see the /foo path-part, because it has already been stripped out by Apache.
Most failures are caused by incorrect patterns; If the pattern does not match the requested URL, the rule is never applied.
Also, be sure to completely flush your browser cache before testing any changes to your code; Stale page responses cached by your browser can invalidate your test results, since the browser will serve the page from its cache (in your computer) instead of sending a request to your server.
Jim
I have one more question (thanks for putting up with me ehre) on what I don't understand in our example.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\?id=([^\ ]+)\ HTTP/ RewriteRule ^index\.php$ http://example.com/article-id-%1 [R=301,L] Why is the condition /index\ and not /article\
So a redirect from www.example.com/article.php?id=1 is going to www.example.php/article-id-1?id=1.
I'm nearly there for all of my floundering, what is a common cause of this issue?
Again, the main cause of such problems is that the patterns in RewriteCond and RewriteRule do not match the requested URL-path and/or query string. These patterns must be exactly precisely correct, or the rule won't run. And again, remember that the URL-path tested by RewriteRule is "localized" to the .htaccess file in which the RewriteRule is located -- the URL-path-part that matches the .htaccess file's current location is removed from the URL-path tested by RewriteRule. Only RewriteCond will see the full requested URL-path.
You might want to back off and simplify: Try writing a rule in the form above that matches one and only one specific URL-path and query string, and redirect it to Google or some other known site. This eliminates the complexity of the back-references (variables) and also eliminates the possibility of looping on your own site. The URL-path you specify can even be a "made-up" (non-existent) URL, so that testing does not interfere with the normal operation of your site.
As an example:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /testrewrite\.php\?id=14\ HTTP/
RewriteRule ^testrewrite\.php$ http://www.google.com/? [R=301,L]
If that does not work, then your RewriteCond is likely missing part of the URL-path info needed to get to the directory in which this .htaccess file is located. Or, there's another rule or directive (in this .htaccess file or one above it, or in the server config files) that is interfering with the execution of this one. There are several Apache modules with directives that can interfere: mod_alias, mod_proxy, mod_dir, mod_negotiation, etc.
You might also want to check your server error log -- The information there might prove very useful.
Jim
Try this for line 2:
RewriteRule ^article\.php$ http://example.com/article-id-%1? [R=301,L]
Adding the ? stops the query string being re-appended.