Forum Moderators: phranque
I need to rewrite some 'friendly' urls so that they can be directed to a php script that will handle the output. More specifically....
RewriteRule recommended23.htm accommodation1.php?SL=1&P=23 [L]
In this case, I need to store the 1-2 digit number in recommendedxx.htm and use it for the P= query parameter.
I tried the following expression but it doesn't work. Where am I going wrong please?
RewriteRule ^recommended([0-9]+).htm$ accommodation1.php?SL=1&P=$1 [L]
The only error message was an error 404 page. However, your code works perfectly. Thanks.
Can you use the same variable-capture techique in a 301 redirect instruction? For example, in the following example I would like to capture the page number (99) and add it to the end of the new page. I tried a variation of your code line, but just got another 404.
redirect 301 /accommodation1/1/99/accommodation.htmhttp://www.mydomain.com/recommended99.htm
Thanks again, either way.
We've had some members here get burned by this problem; It's a major concern if you do not control your server configuration file and that means it concerns most Webmasters, who are on shared hosting and have no access to the config files.
For this reason, it really is better to use mod_rewrite for all if you use mod_rewrite for any. Consider it "future-proofing."
Jim
We've had a major upgrade and reorganisation of the site meaning a) lots of rewrites necessary to translate 'friendly' URLs to scripts, and b) permanent redirects of old URLS. So, following my changes in this topic, my htaccess file is now littered with numerous of the following five types of general command:
1) RewriteCond %{HTTP_USER_AGENT} ^(aesop_com_spiderman地lexibot地narchie地nonymouse地spseek地sterias地ttach) [NC,OR]
2) redirect 301 /restaurants01c.php [mydomain.com...]
3) RedirectMatch 301 ^/?accommodation1/1/([0-9]+)/recommended_accommodation\.htm$ [mydomain.com...]
4) RewriteRule ^recommended([0-9]+)\.htm$ /accommodation1.php?SL=1&P=$1 [L]
5) RewriteRule hotels/village-haven.htmaccommodation2a.php?P=8 [L]
So, my question is: which of these 5 lines should be changed and can someone please help with the correct syntax which I can study and apply to other lines of that category.
I really appreciate the help I'm getting, even if I am ending up with a headache. :)
2) redirect 301 /restaurants01c.php http://www.example.com/restaurants01.php
RewriteRule ^resrestaurants01c\.php$ http://www.example.com/restaurants01.php [R=301,L] 3) RedirectMatch 301 ^/?accommodation1/1/([0-9]+)/recommended_accommodation\.htm$ http://www.example.com/recommended$1.htm
RewroteRule ^accommodation1/1/([0-9]+)/recommended_accommodation\.htm$ http://www.example.com/recommended$1.htm [R=301,L] Note the absence of leading slashes on the RewriteRule patterns when used in .htaccess. Also note that RewriteRule is very similar to RedirectMatch.
Be aware that when using the Redirect directive, it works like this:
Redirect /old_path<and more path info may be here in the requested URL-path> http://example.com/new_path<and anything after the requested and matched URL-path that wasn't mentioned in the path-prefix appears here>
That is, the Redirect directive uses prefix-matching, and anything in the requested URL that is beyond the matched prefix in the directive gets added onto the end of the new URL specified on the right side of the directive. This is a common reason that folks have trouble with the Redirect directive, and especially if they've never looked at the mod_alias documentation.
As an example, your first Redirect
Redirect 301 /restaurants01c.php http://www.example.com/restaurants01.php Jim