我正在尝试进行双重定向并且无法获得正确的逻辑:
1。如果页面存在,请忽略所有规则
2. 如果页面不存在,请添加.php扩展名(但将其隐藏在网址栏中)
3. 如果页面不存在且不存在.php扩展名重定向到单个页面(query.php)并附加输入的内容url bar作为可变参数(并在URL栏中点击)。
示例文件结构:
的index.php
pricing.php
query.php
示例测试:
[domain]/pricing.php -> [domain]/pricing (showing the pricing.php page)
[domain]/pricing -> [domain]/pricing (showing the pricing.php page)
[domain]/somethingelse -> [domain]/somethingelse (showing the query.php page with ?somethingelse appended)
的.htaccess
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ query.php?$1 [L]
实际测试:
好
[domain]/pricing.php -> [domain]/pricing (showing the pricing.php page)
[domain]/pricing -> [domain]/pricing (showing the pricing.php page)
错误
[domain]/somethingelse -> [domain]/somethingelse (showing the query.php page with ?somethingelse_php appended)
我上面的.htaccess文件执行了我想要的所有操作,除非我在重定向到query.php时吐出$ _GET变量,它显示" _php"追加。我因为早先的规则而假设......
答案 0 :(得分:0)
请尝试以下方法。这些条件/规则集特定于您列出的要求,并为您提供对用户提供的内容的细粒度控制。
RewriteEngine On
# Serve the requested file if it exists
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# If not, then check if its .php equivalent exists
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.+)/?$ $1.php [L]
# Otherwise, send the request off to query.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)/?$ /query.php?$1 [L]
就最后一条规则而言,如果/testing
不存在var_sump($_GET)
,则query.php
使用/testing.php
会导致请求array(1) { ["testing"]=> string(0) "" }
转储:
Mat
答案 1 :(得分:0)
请尝试以下规则
RewriteEngine On
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
RewriteCond %{REQUEST_URI} !^/query\.php
RewriteRule ^(.*)$ /query.php?$1 [L,R]