htaccess不适用于子文件夹

时间:2012-09-24 23:08:47

标签: .htaccess mod-rewrite hyperlink rewrite subdirectory

我想问为什么根文件夹中的.htaccess不能正常工作。

的.htaccess

RewriteEngine On 
RewriteRule ^([a-zA-Z0-9]+)(/[a-zA-Z0-9/]+)?$ /$1\.php$2 [L]

RewriteCond %{THE_REQUEST} ^GET\s/*pages/.*\.php [NC] 
RewriteRule ^([a-zA-Z0-9]+)(/[a-zA-Z0-9/]+)?$ /$1\.php$2 [L]

RewriteCond %{THE_REQUEST} ^GET\s/*debates/.*\.php [NC]
RewriteRule ^([a-zA-Z0-9]+)(/[a-zA-Z0-9/]+)?$ /$1\.php$2 [L]

/(主文件夹)中的链接与.php hidding有关,但在/ pages /和/ debates /等子文件夹中,系统无法识别重写规则。我想在两个目录中隐藏.php,就像它们隐藏在主文件夹中一样。

我找到了解决方案!

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]

1 个答案:

答案 0 :(得分:0)

看起来这些规则都是错误的,他们不会做任何事情:

RewriteCond %{THE_REQUEST} ^GET\s/*pages/.*\.php [NC] 
RewriteRule ^([a-zA-Z0-9]+)(/[a-zA-Z0-9/]+)?$ /$1\.php$2 [L]

RewriteCond %{THE_REQUEST} ^GET\s/*debates/.*\.php [NC]
RewriteRule ^([a-zA-Z0-9]+)(/[a-zA-Z0-9/]+)?$ /$1\.php$2 [L]

您可以将htaccess更改为:

RewriteEngine On 
RewriteRule ^pages/([a-zA-Z0-9]+)(/[a-zA-Z0-9/]+)?$ /pages/$1\.php$2 [L]
RewriteRule ^debates/([a-zA-Z0-9]+)(/[a-zA-Z0-9/]+)?$ /debates/$1\.php$2 [L]
RewriteRule ^([a-zA-Z0-9]+)(/[a-zA-Z0-9/]+)?$ /$1\.php$2 [L]