Where are thou, oh RichardK

Discuss practical ways rearrange URLs using mod_rewrite.

Where are thou, oh RichardK

Postby AbuAaminah » Thu Feb 05, 2009 2:32 am

Hi Richard,

Sometime back (although I can't find the threads), you helped me to come up with the following:

# Remove extensions for security through obscurity
###################################
RewriteRule ^(/.+)\.(php|asp|html|jsp|htm|ece|aspx)$ $1/ [R=301,L]

# Redirect /name to /name/ if a PHP file or directory exists
###################################
RewriteCond %{DOCUMENT_ROOT}$1.php -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} -d
RewriteRule ^(/.*[^/])$ $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 %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} !-d
RewriteRule ^(/.*[^/])/$ $1.php [QSA,L]

This all worked when it was placed in the httpd.conf file, but due to a restriction I've had to put it into an htaccess file - where it doesn't work anymore.

Please help me... what needs to be changed to make it work via htaccess?

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

Postby richardk » Thu Feb 05, 2009 2:34 am

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^(.+)\.(php|asp|html|jsp|htm|ece|aspx)$ $1/ [R=301,L]

RewriteCond %{DOCUMENT_ROOT}/$1.php -f [OR]
RewriteCond %{SCRIPT_FILENAME} -d
RewriteRule ^(.*[^/])$ /$1/ [R=301,L]

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

Postby AbuAaminah » Thu Feb 05, 2009 2:37 am

My God that was fast! You're not related to Batman are you?

Meanwhile, they all seem to work. However, when the redirect takes place, I get the whole path to the file along with the new url appended to the host name.

Any ideas?
AbuAaminah
 
Posts: 40
Joined: Mon Dec 08, 2008 6:31 pm

Postby AbuAaminah » Thu Feb 05, 2009 2:44 am

I added "RewriteBase /" and, although it now redirects to the right url, I get endless loops.

Any ideas?
AbuAaminah
 
Posts: 40
Joined: Mon Dec 08, 2008 6:31 pm

Postby richardk » Thu Feb 05, 2009 2:47 am

This should fix it
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.(php|asp|html|jsp|htm|ece|aspx)$ /$1/ [R=301,L]

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*[^/])$ /$1/ [R=301,L]

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

Postby AbuAaminah » Thu Feb 05, 2009 2:55 am

Pure unadulterated genius!

Millions and millions of thanks.
AbuAaminah
 
Posts: 40
Joined: Mon Dec 08, 2008 6:31 pm

Postby AbuAaminah » Thu Feb 05, 2009 3:27 am

I've just hit a snag...

When a form is submitting to /path-to/forms-processor/, all the form values to disappear.

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

Postby AbuAaminah » Thu Feb 05, 2009 3:28 am

I take my words back... that's not the problem... the values reach fine...

I will post back if I can't fix the issue myself.

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


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 35 guests

cron