我有跟随htaccess重写规则,我想改为无扩展网址,它工作得很好,但在子目录中使用时存在缺陷,
例如:
除非有www.domain.com/cp/login
,否则 www.domain.com/cp/login.php
不会显示该页面
www.domain.com/contact_us
工作得很棒。
请帮助解决以下有关如何在根目录和子目录上无扩展工作的规则?
DirectoryIndex index.php
Options -Indexes
<Files *htaccess>
Deny from all
</Files>
<files page>
ForceType application/x-httpd-php
</files>
<IfModule mod_rewrite.c>
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#resolve .php file for extensionless php urls
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^cp/edit-agent/([^/\.]+)/?$ cp/edit-agent.php?name=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/\.]+)/?$ agent.php?name=$1 [L]
RewriteRule ^contact/([^/\.]+)/?$ contact.php?a=$1 [L]
#rule to handle example.com/123/sys
RewriteRule ^project/([0-9]+)/([^/\.]+)/?$ project.php?a=$1&t=$2 [L,QSA]
</IfModule>
修改
DirectoryIndex index.php
Options -Indexes
<Files *htaccess>
Deny from all
</Files>
<files page>
ForceType application/x-httpd-php
</files>
<IfModule mod_rewrite.c>
Options +SymLinksIfOwnerMatch
RewriteEngine On
#RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
#resolve .php file for extensionless php urls
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
#RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteRule ^cp/edit-agent/([^/\.]+)/?$ cp/edit-agent.php?name=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/\.]+)/?$ agent.php?name=$1 [L]
RewriteRule ^contact/([^/\.]+)/?$ contact.php?a=$1 [L]
#rule to handle example.com/123/sys
RewriteRule ^project/([0-9]+)/([^/\.]+)/?$ project.php?a=$1&t=$2 [L,QSA]
</IfModule>
答案 0 :(得分:1)
由于.htaccess位于子目录中,因此您需要使用RewriteBase
并在/_agent/
前面使用相同的%{DOCUMENT_ROOT}
:
DirectoryIndex index.php
Options -Indexes
<Files *htaccess>
Deny from all
</Files>
<files page>
ForceType application/x-httpd-php
</files>
<IfModule mod_rewrite.c>
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /_agent/
RewriteRule ^cp/edit-agent/([^/.]+)/?$ cp/edit-agent.php?name=$1 [L,QSA]
RewriteRule ^contact/([^/.]+)/?$ contact.php?a=$1 [L,QSA]
#rule to handle example.com/123/sys
RewriteRule ^project/([0-9]+)/([^/.]+)/?$ project.php?a=$1&t=$2 [L,QSA]
#resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/_agent/$1\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)/?$ agent.php?name=$1 [L,QSA]
</IfModule>