Handling extensionless URI's

Discuss practical ways rearrange URLs using mod_rewrite.

Handling extensionless URI's

Postby AbuAaminah » Mon Dec 22, 2008 7:06 pm

Hi,

I am trying to get rid of the .php extension in all of a site's URI's using the following rewrite:

RewriteCond %{REQUEST_URI} !^(.*)/$
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteRule !(\.)(.*)$ %1.php [L]

Whilst the above does work, it brings with it 1 problem and 1 impracticality, the two of which would hopefully be resolved simultaneously:

The problem

The problem is that if the extensionless URI belongs to a valid directory name, it returns a 404 instead of adding a slash to return the directory.

The impracticality

The impracticality is that many people might not realise that a URI has a slash at the end or not and they might key the URI in with or without the slash. Keying it in with the slash would return a 404 if a directory didn't exist.

The best solution in my opinion, if possible, would be to use a rewrite to redirect slashed uri's to their non-slash equivalet.

Then to use another rewrite to do the following:
1. If I add ".php" to this request and it resolves to a real file, then show the real file.
2. Otherwise, if i add a slash and it resolves to a real directory, then show the directory's index file instead.

I hope you can help.

Many thanks
AbuAaminah
 
Posts: 40
Joined: Mon Dec 08, 2008 6:31 pm

Postby richardk » Tue Dec 23, 2008 9:17 am

You should not do it based on a trailing slash, you should check the PHP files exists using the -f flag (and that a directory does not using !-d).

Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Remove the .php from PHP files (only files
# that exist).
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule ^(.+)\.php$ /$1/ [R=301,L]

# Rewrite /<name and /<name>/ to /<name>.php if
# a PHP file exists and a directory does not.
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*[^/])/?$ /$1.php [QSA,L]

(For a .htaccess file, this has not been tested when Inheriting into a <VirtualHost>.)
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby AbuAaminah » Tue Dec 23, 2008 9:41 am

Hi Richard,

Although, I've only tested in htaccess as well, things seem to work well.

The only problem is that files can be reached without their trailing slashes as well as with.

How can we redirect /directory/file to /directory/file/ so that the file is only accessed with the trailing slash?

Thanks again.
AbuAaminah
 
Posts: 40
Joined: Mon Dec 08, 2008 6:31 pm

Postby richardk » Tue Dec 23, 2008 9:44 am

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# Remove the .php from PHP files (only files
# that exist).
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} -f
RewriteRule ^(.+)\.php$ /$1/ [R=301,L]

# Redirect  /<name to /<name>/ if a PHP file
# or directory exists.
RewriteCond %{DOCUMENT_ROOT}/$1.php -f [OR]
RewriteCond %{SCRIPT_FILENAME} -d
RewriteRule ^(.*[^/])$ /$1/ [R=301,L]

# Rewrite /<name>/ to /<name>.php if
# a PHP file exists and a directory does not.
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*[^/])/$ /$1.php [QSA,L]
Last edited by richardk on Tue Dec 23, 2008 10:35 am, edited 1 time in total.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 24 guests

cron