removing file extension

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

removing file extension

Postby bendude » Thu Jul 09, 2009 7:18 am

Ok so i have a few rules and i think im nearly their, just need a guru to help me fix this last issue..

I have a folder containting my files with the extension .php i would like it to look like this in the url /filename/ which points to filename.php

The below sort of works but when the file does not exist it gets into an infinite loop??

Code: Select all

#match everything until the slash and then point to same name .php
#make sure its not a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1.php [L]


I would also like rewrite the url if people try to access filename.php back to /filename/.

Thanks for the help.

PS, i am using .htaccess
bendude
 
Posts: 5
Joined: Thu Jul 09, 2009 7:10 am

Postby richardk » Thu Jul 09, 2009 10:28 am

Post all your mod_rewrite.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby bendude » Thu Jul 09, 2009 5:17 pm

ok i have two files... everything is routed from my root directory to a public with this file

Code: Select all
Options +FollowSymlinks

RewriteEngine On
   RewriteRule    ^$    public/    [L]
   RewriteRule    (.*) public/$1    [L]


and then this is my main one inside the public folder

Code: Select all

Options +FollowSymlinks


RewriteEngine On
RewriteBase /mweb/

#removes index and index.php when access the homepage
RewriteCond %{THE_REQUEST} \ /(.+/)?index(\.php)?(\?.*)?\  [NC]
RewriteRule ^(.+/)?index(\.php)?$ /%1 [R=301,L]


#check if the file requested exists, if it does do nothing
RewriteCond %{REQUEST_FILENAME} !-f

#check if it ends with a slash, if it does do nothing
RewriteCond %{REQUEST_URI} !(.*)/$

#otherwise add that trailing slash
RewriteRule ^(.*)$ http://localhost/mweb/$1/ [R=301,L]

#match everything until the slash and then point to same name .php
#make sure its not a directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1.php [L]


i should probably mention i am developing locally hence the localhost in their... and the site's root directory on my localhost is mweb, thats why i have the rewrite base etc. but when the site goes live it will not be in a mweb directory..

Thanks
Ben
bendude
 
Posts: 5
Joined: Thu Jul 09, 2009 7:10 am

Postby richardk » Fri Jul 10, 2009 9:41 am

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

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

# Only continue if this is a new request (not an internal mod_rewrite/Apache request).
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule [^/]$ http://localhost%{REQUEST_URI}/ [R=301,L]

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.+)/$ $1.php [L]


i should probably mention i am developing locally hence the localhost in their... and the site's root directory on my localhost is mweb, thats why i have the rewrite base etc. but when the site goes live it will not be in a mweb directory..

You may be interested in this: Testing many websites on your development computer/server.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby bendude » Fri Jul 10, 2009 7:56 pm

Thanks, that works and stops it looping when i enter an incorrect filename, but it no longer adds the slash if i try to reach http://localhost/mweb/filename


also would it be possible to rewrite filename.php back to filename/ I just don't want to get penalised for duplicate content.

Thanks
Ben
bendude
 
Posts: 5
Joined: Thu Jul 09, 2009 7:10 am

Postby richardk » Sun Jul 12, 2009 2:57 pm

Hmm, try
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]

# Add trailing slash.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*[^/])$ %{REQUEST_URI}/ [R=301,L]

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

Postby bendude » Sun Jul 12, 2009 7:48 pm

Thanks for all the help, I don't know why but this does not work for filename/ or just filename i can only access the files through filename.php.

Any suggestions why it would not be working?

Thanks
Ben
bendude
 
Posts: 5
Joined: Thu Jul 09, 2009 7:10 am

Postby richardk » Wed Jul 15, 2009 12:30 pm

All
Code: Select all
%{DOCUMENT_ROOT}/$1.php

should be
Code: Select all
%{DOCUMENT_ROOT}/mweb/$1.php
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby bendude » Wed Jul 15, 2009 5:25 pm

Still no look with them rules, the closest i have managed with some research on the internet is this..

Code: Select all
#add trailing slash and point to php file

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]


Which adds the forward slash and points to the php file but does not remove .php

Thanks
Ben
bendude
 
Posts: 5
Joined: Thu Jul 09, 2009 7:10 am

Postby richardk » Thu Jul 16, 2009 1:46 pm

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]

# Add trailing slash.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule ^(.*[^/])$ %{REQUEST_URI}/ [R=301,L]

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

should work.
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 91 guests

cron