0

I'm having trouble with mod_rewrite. I have a basic PHP website, where all files have the .php extension.

What I want to happen is:

www.site.example/file and www.site.example/file/

both serve up www.site.example/file.php, but without showing the extension (so the URL bar shows www.site.example/file)

and if someone types in www.site.example/file.php, it redirects to www.site.example/file

My .htaccess code is as follows:

Options +FollowSymLinks

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^([^/]+)/$ /$1 [R=301,L]

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

RewriteRule ^([^/.]+)$ $1.php [L]

# Rules For file inside subfolders like
# /folder/myfile --> /folder/myfile.php
# and
# /folder/myfile/ --> /folder/myfile
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/([^/]+)/$ /$1/$2 [R=301,L]
RewriteRule ^(.+)/([^/.]+)$ $1/$2.php [L]

The first two (/file and /file/ serving file.php) work fine, but if I type in www.site.example/file.php, the browser redirects to:

http://www.site.example/home/siteuser/public_html/file

and gives a 404 error

The rewrite log shows the following (loglevel 2):

[08/Jan/2013:17:35:33 +0000] [www.mysite.example/sid#b970c168][rid#b9914e08/initial] (2) [perdir /home/mysite.example/public_html/] rewrite 'about-us.php' -> 'about-us'
[08/Jan/2013:17:35:33 +0000] [www.mysite.example/sid#b970c168][rid#b9914e08/initial] (2) [perdir /home/mysite.example/public_html/] explicitly forcing redirect with http://www.mysite.example/home/mysite.example/public_html/about-us
[08/Jan/2013:17:35:33 +0000] [www.mysite.example/sid#b970c168][rid#b9914e08/initial] (1) [perdir /home/mysite.example/public_html/] escaping http://www.mysite.example/home/mysite.example/public_html/about-us for redirect
[08/Jan/2013:17:35:33 +0000] [www.mysite.example/sid#b970c168][rid#b9914e08/initial] (1) [perdir /home/mysite.example/public_html/] redirect to http://www.mysite.example/home/mysite.example/public_html/about-us [REDIRECT/301]
[08/Jan/2013:17:35:33 +0000] [www.mysite.example/sid#b970c168][rid#b9914e08/initial] (2) [perdir /home/mysite.example/public_html/] rewrite 'home/mysite.example/public_html/about-us' -> 'home/mysite.example/public_html/about-us.php'
[08/Jan/2013:17:35:33 +0000] [www.mysite.example/sid#b970c168][rid#b9914e08/initial] (2) [perdir /home/mysite.example/public_html/] strip document_root prefix: /home/mysite.example/public_html/home/mysite.example/public_html/about-us.php -> /home/mysite.example/public_html/about-us.php
[08/Jan/2013:17:35:33 +0000] [www.mysite.example/sid#b970c168][rid#b9914e08/initial] (1) [perdir /home/mysite.example/public_html/] internal redirect with /home/mysite.example/public_html/about-us.php [INTERNAL REDIRECT]
[08/Jan/2013:17:35:33 +0000] [www.mysite.example/sid#b970c168][rid#b991e190/initial/redir#1] (1) [perdir /home/mysite.example/public_html/] pass through /home/mysite.example/public_html/home
[08/Jan/2013:17:35:34 +0000] [www.mysite.example/sid#b970c168][rid#b9914e08/initial] (1) [perdir /home/mysite.example/public_html/] pass through /home/mysite.example/public_html/favicon.ico

How do I stop it from redirecting to the file path, and make it redirect to www.site.example/file instead?

Stephen Ostermiller
  • 23,933
  • 14
  • 88
  • 109
Iain
  • 471
  • 5
  • 14
  • Duplicate of [How to remove file extension from website address? (sample photos attached)](http://stackoverflow.com/questions/6534904/how-to-remove-file-extension-from-website-address-sample-photos-attached) – Jason McCreary Jan 08 '13 at 17:03
  • Sometimes, just enabling rewrite logging with `RewriteLog` and `RewriteLogLevel` can make debugging this sort of thing much easier. – ldx.a.ldy.c Jan 08 '13 at 17:03
  • @jason I did look at that one, but it seems like a different set of rules - that one didn't handle trailing slashes – Iain Jan 08 '13 at 17:34
  • @ldx.a.ldy.c thanks - I've done that and added the output to my question. Still not sure what's going on though! – Iain Jan 08 '13 at 17:36
  • Let's say redirecting `www.site.com/file.php` to `www.site.com/file` is accomplished. ¿Does the `file` directory exists? ¿Is there a resource (script or similar) that handles the requests to that directory? If yes, you are just redirecting one script to another, which doesn't makes much sense with rewrite rules. If not, what you are trying to do makes no sense either and will never work. It should be the opposite: `www.site.com/file` is the entered URL, redirected internally to a resource like `www.site.com/file.php` – Felipe Alameda A Jan 08 '13 at 20:06

0 Answers0