I’m not too good with mod_rewrite or regular expressions, but I need to ultimately use 301 redirects for a site I am moving. The structure of the old site is as so
http://www.mywebsite.com/FF_Supersneaker_Black_Metallica_p/155-157.htm
and the new site for the same product is as so
https://www.mywebsite.com/FF_Supersneaker_Black_Metallica_p.html
What I need to do is extract and remove the “/155-157” and change the extension to .html instead of .htm
So Logically I guess I would need to find the last occurrence of a “/” and the last occurrence of a “.” and change the value of everything in between to “”. then the changing of the file extension is everywhere on the internet, but I don’t know how I would incorporate it with the rest of it.
edit:
Here is my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule /(.*)/(.*)/[^/]*.htm$ /$1$2.html
</IfModule>
And here is the associated entry in my rewritelog when I tried to use it. (I just changed the ip and domain names. I used tail and copied the entry that popped up when I tried going to the page.
255.255.255.255 - - [25/Apr/2012:11:57:24 --0400] [www.mywebsite.com/sid#9b25ed8][rid#9e3cc48/initial] (1) [perdir /var/www/test/] pass through /var/www/test/FF_Supersneaker_Black_Metallica_p/155-157.htm
I added the extra /(.*) because there is an extra directory on my test page. The structure for that is
http://www.mywebsite.com/test/FF_Supersneaker_Black_Metallica_p/155-157.htm
to redirect to
http://www.mywebsite.com/test/FF_Supersneaker_Black_Metallica_p.html
Sorry for asking so many questions, also you can only enable rewrite logs buy going into the apache.conf or httpd.conf files and restarting the server, right? If that is the case then I don’t have the permissions.
All you’re doing here is stripping off the last path component and then adding “.html”. So maybe something like this:
The sequence
[^/]means “any character not a/“, and*means “the previous pattern 0 or more times”, so[^/]*.htmmatches any string of non-/characters followed by.htmat the end of a string. We discard these, use everything else, and add.html.The URL Rewriting Guide has lots of information and examples.