Beginner to mod_rewrite, having trouble with some redirects

New to mod_rewrite? This is a good place to start.

Beginner to mod_rewrite, having trouble with some redirects

Postby smcl1985 » Sun Sep 27, 2009 2:27 pm

Hi there,

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 :)
smcl1985
 
Posts: 6
Joined: Sun Sep 27, 2009 1:33 pm

Postby richardk » Sun Sep 27, 2009 2:42 pm

Or you could redirect to remove the .php extension
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Redirect to remove /index.php files.
RewriteCond %{THE_REQUEST} \ /(.+/)?index\.php(\?.*)?\  [NC]
RewriteRule ^(.+/)?index\.php$ /%1 [NC,R=301,L]

# Redirect to remove .php
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule ^(.+)\.php$ /$1/ [R=301,L]

# Specific rules.
RewriteRule ^(holiday-villa-rentals)/([0-9]+)/?$ /$1.php?id=$1 [QSA,L]

# Rewrite to add .php back.
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*[^/])/$ /$1.php [QSA,L]


To rewrite to /notfound/ replace
Code: Select all
# Redirect to remove .php
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule ^(.+)\.php$ /$1/ [R=301,L]

with
Code: Select all
# Rewrite .php requests to /notfound/
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule ^(.+)\.php$ /notfound/ [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby smcl1985 » Mon Sep 28, 2009 5:29 am

Thanks for this, I'll try it out when I get back from work tonight :)

Just wondering why I'd need to put this in at the end?

Code: Select all
# Rewrite to add .php back.
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*[^/])/$ /$1.php [QSA,L]


Thanks again.
smcl1985
 
Posts: 6
Joined: Sun Sep 27, 2009 1:33 pm

Postby richardk » Mon Sep 28, 2009 2:21 pm

It takes the request to /browse and rewrites it to /browse.php.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby smcl1985 » Tue Sep 29, 2009 12:44 am

Ah ok.

I tried the solution you suggested above but when going to /holiday-villa-rental/31 the page won't recognise the querystring "id" :confused:

I'm not sure if the rewrite is correct. What I'd like to do is be able to visit /holiday-villa-rental/31 instead of what is currently working: /holiday-villa-rental/?id=31

Thanks :)
smcl1985
 
Posts: 6
Joined: Sun Sep 27, 2009 1:33 pm

Postby richardk » Tue Sep 29, 2009 2:43 pm

Try replacing
Code: Select all
Options +FollowSymLinks

with
Code: Select all
Options +FollowSymLinks -MultiViews
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 26 guests

cron