我正在重写特定页面访问的请求,但我想在几页中添加一个排除项。让我们说,我想请求site.com/page/test转到site.com/page.php?str=test,但我想在一个字符串中添加一个排除项,但不会转到该页面,而是另一个页面,例如:site.com/page/contact应该转到site.com/contact.php
这就是我的尝试:
RewriteRule ^static/mobile_app/?$ static/mobile_app.php [QSA,L]
RewriteRule ^static/([a-zA-Z0-9%.\-_]+)/?$ static/page.php?url_key=$1 [QSA,L]
我在文件末尾也有这个:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? /static/page.php?url_key=404 [L]
有一件奇怪的事情是,如果我尝试以下规则,它就会起作用:
RewriteRule ^static/mobile_app.php$ static/mobile_app.php [QSA,L]
RewriteRule ^static/([a-zA-Z0-9%.\-_]+)/?$ static/page.php?url_key=$1 [QSA,L]
意思是,如果我尝试用扩展名编写完整的文件名,它就可以了。有人可以帮忙吗?感谢。
答案 0 :(得分:2)
让您的完整DOCUMENT_ROOT/.htaccess
像这样:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteRule ^static/([\w.-]+)/?$ static/page.php?url_key=$1 [QSA,L,NC]
## To internally redirect /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /static/page.php?url_key=404 [L,QSA]