I very recently started to use mod_rewrite and I'm having some trouble getting a couple of redirects to work properly. What I've managed to do so far is remove '.php' extensions so '/browse.php' becomes '/browse', as well as force trailing slashes so '/browse' becomes '/browse/'.
The code I have for this is...
- Code: Select all
# remove php file extensions
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
# force a trailing slash to be added
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
What I'd also like to do is forward any addresses with php file extensions to my 404 error page /notfound/ so to not create duplicate pages on search engines.
The last thing I want to do is be able to handle query strings properly. Right now /holiday-villa-rentals/?id=40 is what is being used to view a villa rental page, but I'd like to shorten this to /holiday-villa-rentals/40. I've tried a few different ways of doing this but haven't found one that works yet.
Hopefully someone on hear that knows this stuff a lot better than me can help me out.
Thanks