[apache] [.htaccess]可选的尾部斜杠

时间:2013-12-12 09:53:19

标签: apache .htaccess

我希望.htaccess文件能够处理可选的尾部斜杠,这样两者都可以

localhost/Stuff/Test
localhost/Stuff/Test/

会奏效。我已经找到了答案,但我发现的只是我需要'/?',我已经把它放了。救命?这是我的.htaccess文件:

<IfModule mod_rewrite.c>
RewriteEngine On    # Turn on the rewriting engine

RewriteCond %{REQUEST_URI} !\.php 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^([a-zA-Z0-9]+)/?$ $1.php [NC] 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule .* custom_404.html [L] 

</IfModule>

目前它只能在没有斜线的情况下工作。

1 个答案:

答案 0 :(得分:0)

尝试使用php扩展隐藏代码:

RewriteEngine On

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

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteRule ^ custom_404.html [L]