Rewrite not applying to sub-directories

Discuss practical ways rearrange URLs using mod_rewrite.

Rewrite not applying to sub-directories

Postby smcl1985 » Wed Sep 30, 2009 11:56 am

Hi there,

I have a small problem with a rewrite rule not applying to sub-directories. What I'm doing is trying to hide .php file extensions. After several days of trying to get this to work along with a combination of other rewrites I've almost managed to get this working (with the kind help from people in this forum) however I have one final issue to clear up.

Almost everything is working correctly, however .php file extensions are still required within sub-directories, so for example summeraway.com/browse will work, but summeraway.com/manage/add won't :confused:

I tried fixing this myself only to stop another of the rewrites from working (the /holiday-villa-rental/ rewrite). Hopefully someone can find a solution, after 3 days of frustration I'd be extremely grateful!

Thanks :)

Code: Select all
Options +FollowSymLinks

RewriteEngine On

# remove (root) php file extensions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php

# force (root) trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301]

# specific rules
RewriteRule ^holiday-villa-rental/([0-9]+)/$  /holiday-villa-rental/?id=$1
RewriteRule ^holiday-villa-rental/([0-9]+)/([0-9]+)/$  /holiday-villa-rental/?id=$1&photo=$2

# send php requests to /notfound/
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)\.php$ /notfound/ [QSA,L]
smcl1985
 
Posts: 6
Joined: Sun Sep 27, 2009 1:33 pm

Postby richardk » Wed Sep 30, 2009 12:42 pm

Code: Select all
^([^/]+)/$

Only matches one slash.

You shouldn't remove all the flags.

Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Force trailing slashes to for non-files.
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*[^/])$ %{REQUEST_URI}/ [R=301,L]

# Send direct .php requests to /notfound/
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule ^(.+)\.php$ /notfound/ [QSA,L]

# Specific rules
RewriteRule ^holiday-villa-rental/([0-9]+)/$           /holiday-villa-rental/?id=$1          [QSA,L]
RewriteRule ^holiday-villa-rental/([0-9]+)/([0-9]+)/$  /holiday-villa-rental/?id=$1&photo=$2 [QSA,L]

# /file/ to /file.php
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule ^(.+)/$ /$1.php [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby smcl1985 » Wed Sep 30, 2009 12:49 pm

Hey Richard,

Thanks very, very much! Your suggestion worked first time and I can't think you enough :)

I notice you use a slightly different syntax to other websites offering mod_rewrite tutorials, would you be able to recommend any mod_rewrite books that reflect this?

Thanks again,

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

Postby richardk » Wed Sep 30, 2009 1:32 pm

I notice you use a slightly different syntax to other websites offering mod_rewrite tutorials

Like what?

would you be able to recommend any mod_rewrite books that reflect this?

I don't know of any mod_rewrite books.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby smcl1985 » Wed Sep 30, 2009 2:38 pm

Nevermind, thanks for the help earlier, I'll go check Amazon for some mod_rewrite books.
smcl1985
 
Posts: 6
Joined: Sun Sep 27, 2009 1:33 pm


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: Yahoo [Bot] and 1 guest

cron