Reputation: 463
There's quite a bit of content available on mod_rewrite but I simply haven't been able to get the following to work.
Right now I have the following code in my htaccess file:
RewriteEngine On
Options -Indexes
Options -Multiviews
ErrorDocument 400 http://localhost/mywebsite/400.php
ErrorDocument 403 http://localhost/mywebsite/403.php
ErrorDocument 404 http://localhost/mywebsite/404.php
ErrorDocument 500 http://localhost/mywebsite/500.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
Which will succesfully make things like mywebsite/index work(instead of mywebsite/index.php). However there are some situations where I will have visitors who will access the site with a trailing slash(mywebsite/index/) and then my rewrite no longer functions. and I receive an error page.
So how will I make it work so any page with either a trailing slash, no slash, or an extension(.php) will work on my website?
I will add that this is on localhost on an apache server(XAMPP) in case this affects how mod_rewrite works.
Upvotes: 2
Views: 356
Reputation: 41249
Replace your code with this :
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [L]
to
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ([^/]+)/?$ $1.php [L]
Upvotes: 2
Reputation: 18671
You can use:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+)/?$ $1.php [L]
Because you can't test before if file (without /) exist or not.
Upvotes: 1
Reputation: 180147
A rewrite rule like this should do the trick:
RewriteRule ^(.*)/?$ $1.php [L]
Upvotes: 0