服务器路径是:
FTP : htaccess 位于 root/
中,index.php
位于 root/www/
中。
真实路径(使用 getcwd): index.php
位于 /home/mysite/www
中。
我正在使用集群网络托管。
这是我编写的(正在运行)具有可读链接的代码:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Https redirect
Options All -Indexes
#Prevents access to server files -- unusual behavior doesn't stop if removed
RewriteRule (^azazaz|placeholdercateg)/?([0-9]+)?/?$ /index.php?category=$1&page=$2 [NC,L]
#Internally redirects mysite.com/placeholdercateg to mysite.com/index.php?category=placeholdercateg or mysite.com/placeholdercateg/pagenumber to mysite.com/index.php?category=placeholdercateg&page=pagenumber
RewriteRule (^azazaz|placeholdercateg)/?(^azaza|placeholderaction)/?([0-9]+)/?([0-9]+)?$ /index.php?category=$1&action=$2&id=$3&postid=$4 [NC,L]
#Same as first RewriteRule, but for user interaction
问题 1:(已解决,见下文)
由于某种原因,如果有人在要重写的 URL 中输入 index.php
(例如:mysite.com/placeholdercateg/index.php
),则 URL 会发生变化本身更改为 mysite.com/www/placeholdercateg
,然后如果他们再次输入 index.php
,它会将自身更改为 mysite.com/www/www/placeholdercateg
无穷无尽。不过,正如预期的那样,mysite.com/index.php
遇到了 404。
问题 2:
另一个我无法理解的异常行为:不存在的页面未遇到 404 错误(例如:mysite.com/icanliterallywriteanything/notevenkidding/placeholdercateg
),该页面仅显示 mysite .com/placeholdercateg
但 URL 仍显示为已更改。
这很令人痛苦,我想解决这些问题。有什么建议吗?
(^azazaz)
的用途是什么?
我不明白为什么,但在我的例子中,RewriteRule 中的第一个语句总是会导致 404 错误,即使它应该存在。例如,如果 RewriteRule 为 (^placeholdercateg)/etc
并且我访问 mysite.com/placeholdercateg
,则会立即返回 404。
这就是为什么我使用“azazaz”作为第一个语句,使用“placeholdercateg”作为第二个语句,因为它仅阻止第一个语句,而该规则适用于第二个语句。我也想了解更多这方面的信息,但这不是我目前最关心的问题。
- 为什么您没有尝试使用现有的文件或文件夹来重写您的网址?
因为没有。我正在使用 MVC 模式。
- 除了这些之外,您还有其他规则吗?
(已添加)
- 提及您的index.php和htaccess的路径以及它们驻留在服务器中的位置。
(已添加)
- 使用 @RavinderSingh13 的解决方案进行尝试 1: https://stackoverflow.com/a/68480103/16499978
尝试: mysite.com/index.php
==> 访问index.php(主页)
尝试: mysite.com/placeholdercateg/index.php
==> 404 未找到(问题 1 已解决)
尝试: mysite.com/anythingreally/reallyanything/etc/placeholdercateg/
==> placeholdercateg 页面已访问
最佳答案
根据您展示的尝试、 sample ;请尝试遵循规则文件中的 htaccess 规则。确保在测试您的网址之前清除浏览器缓存。
RewriteEngine On
Options All -Indexes -MultiViews
#Https redirect
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index.php/?$ - [F,NC,L]
#Prevents access to server files -- unusual behavior doesn't stop if removed
#Internally redirects mysite.com/placeholdercateg to mysite.com/index.php?category=placeholdercateg or mysite.com/placeholdercateg/pagenumber to mysite.com/index.php?category=placeholdercateg&page=pagenumber
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (^azazaz|placeholdercateg)/?([0-9]+)?/?$ /home/mysite/www/index.php?category=$1&page=$2 [NC,L]
#Same as first RewriteRule, but for user interaction
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (^azazaz|placeholdercateg)/?(^azaza|placeholderaction)/?([0-9]+)/?([0-9]+)?$ /home/mysite/www/index.php?category=$1&action=$2&id=$3&postid=$4 [NC,L]
##New rules for non-existing uris.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*/([\w-]+)/?$ /$1? [R=301,L]
RewriteRule ^(.*)/?$ index.php?param=$1 [QSA,L]
关于apache - Htaccess : URL Rewriting not causing 404 on nonexisting pages and not working as expected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68479746/