htaccess重写规则与一个变量不工作



该规则将我们带到错误页面RewriteRule ^latest/([A-Za-z0-9]+)$ latest?auth=$1 [NC,L]

在我的。htaccess文件

中有以下内容
# BEGIN - Allow Sucuri Services
<IfModule mod_rewrite.c>
RewriteRule ^sucuri-(.*).php$ - [L]
</IfModule>
# END - Allow Sucuri Services
<Files 403.shtml>
order allow,deny
allow from all
</Files>
ErrorDocument 404 /404.php
Options +FollowSymLinks
Options +MultiViews
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} !^www.xxxxx.com$ [NC]
RewriteRule ^(.*)$ https://www.xxxxx.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ci_index.php?/$1 [L]
## Remove php extension
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteRule ^([^.]+)$ $1.php [NC,L]
RewriteRule ^latest/([A-Za-z0-9]+)$ latest?auth=$1 [NC,L]

使用以下规则

RewriteRule ^latest/([A-Za-z0-9]+)$ latest?auth=$1 [NC,L]

试图实现以下目标:

https://www.xxxxx.com/latest?auth=US-mobile-county

https://www.xxxxx.com/latest/US-mobile-county

该规则将我们带到错误页面RewriteRule ^latest/([A-Za-z0-9]+)$ latest?auth=$1 [NC,L]

你没有明确说明什么是"错误页面"。你指的是?或者期望处理此请求的内容。这个指令本身是不正确的,所以不能立即清楚你要做的是什么。我假设意图是重写为latest.php(而不是此规则所建议的latest,并在稍后的问题中提到)-因为这似乎是实现此类规则的唯一原因(您的问题标记为php)。通过重写为latest,只有您依赖于附加.php扩展名的其他指令-其中存在冲突。

发布的指令有许多问题,这些问题阻止了这一工作。值得注意的是,规则的顺序是错误的,MultiViews的使用(可能是为了使无扩展的url工作)使问题复杂化。事实上,看起来问题中的规则根本没有被处理。

没有MultiViews,并且由于指令的顺序,形式/latest/something的请求将被重写为/ci_index.php?/latest/something(可能是CodeIgniter前置控制器),我猜这将导致CI生成404响应。然而,由于MultiViews已经启用,mod_negotiation优先重写。请求/latest.php/something,它不匹配任何你的规则,所以要么导致404(取决于你的服务器配置)或调用latest.php,但没有任何URL参数,这可能会导致你的脚本失败?

https://www.xxxxx.com/latest/US-mobile-county

另外,请注意您的示例URL包含连字符(-),但您的指令中的正则表达式(即。^latest/([A-Za-z0-9]+)$)不允许使用连字符,因此无论如何都无法匹配。

试试下面的命令,替换ErrorDocument指令之后的所有内容:

# Disable MultiViews
Options +FollowSymLinks -MultiViews
RewriteEngine on
# Redirect HTTP to HTTPS
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Redirect non-www to www
RewriteCond %{HTTP_HOST} !^www.example.com [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]
# Rewrite "/latest/something" to "/latest.php?auth=something"
RewriteRule ^latest/([A-Za-z0-9-]+)$ latest.php?auth=$1 [L]
# Allow extensionless PHP URLs to work
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^.]+)$ $1.php [L]
# Front-controller
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ci_index.php?/$1 [L]

注意,我已经颠倒了指令的顺序,所以现在有问题的规则是第一个,而CI前端控制器现在是最后一个。.htaccess中指令的顺序很重要。

由于您已经启用了MultiViews(现在在上面禁用了),您启用PHP无扩展url的规则(您已经标记为"删除PHP扩展")实际上根本没有被使用(除非您的目录或文件包含点,而不是用于分隔文件扩展名的点)。

最新更新