简体   繁体   中英

.htaccess allow extension, allow no extension, and allow trailing slash

I am having trouble coding my htaccess file to do what I need it to do.

Here is what I have so far:

RewriteOptions inherit
ErrorDocument 404 http://www.tempsite.com/400.php
ErrorDocument 500 http://www.tempsite.com/500.php

RewriteEngine On

RewriteRule ^index/?$ index.php  [NC]
RewriteRule ^pricing/?$ pricing.php  [NC]
RewriteRule ^support/?$ support.php  [NC]
RewriteRule ^about/?$ about.php  [NC]
RewriteRule ^learning/?$ learning.php  [NC]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L]

So from my testing and my knowledge what this does is:

  • sets up a 404 and 500 error page redirect
  • any pages that end with a slash, redirect to .php
  • any pages that end with a any extension, redirect to .php

This does what im wanting HOWEVER I don't want to have to add each page like: RewriteRule ^index/?$ index.php [NC] every time I create a new page so how do I write a RewriteRule that does the same thing (allows the page with a slash and without) but for all domain pages?

You can use:

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

Instead of all lines from RewriteRule ^index/?$ index.php ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM