Htaccess :URL 重写不会在不存在的页面上导致 404 并且无法按预期工作
Htaccess : URL Rewriting not causing 404 on nonexisting pages and not working as expected
服务器路径是:
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=&page= [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=&action=&id=&postid= [NC,L]
#Same as first RewriteRule, but for user interaction
问题 1:(已解决,见下文)
出于某种原因,如果有人在 URL-to-be-rewritten 中输入 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”用作第二个语句的原因,因为它仅阻止第一个语句,而该规则适用于第二个语句。我也想了解更多,但这不是我目前最关心的问题。
- 你为什么不尝试使用已经存在的文件或文件夹来重写你的 URLs?
因为没有。我正在使用 MVC 模式。
- 除了这些还有其他规则吗?
(已添加)
- 提及您的 index.php 和 htaccess 的路径以及它们在服务器中的位置。
(已添加)
- 尝试 1 @RavinderSingh13 的解决方案:
尝试: mysite.com/index.php
==> index.php 被访问(主页)
尝试: mysite.com/placeholdercateg/index.php
==> 404 未找到(问题 1 已解决)
尝试: mysite.com/anythingreally/reallyanything/etc/placeholdercateg/
==> placeholdercateg 页面被访问
根据您展示的尝试,示例;请尝试在您的规则文件中遵循 htaccess 规则。确保在测试您的 URL 之前清除您的浏览器缓存。
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=&page= [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=&action=&id=&postid= [NC,L]
##New rules for non-existing uris.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*/([\w-]+)/?$ /? [R=301,L]
RewriteRule ^(.*)/?$ index.php?param= [QSA,L]
服务器路径是:
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=&page= [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=&action=&id=&postid= [NC,L]
#Same as first RewriteRule, but for user interaction
问题 1:(已解决,见下文)
出于某种原因,如果有人在 URL-to-be-rewritten 中输入 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”用作第二个语句的原因,因为它仅阻止第一个语句,而该规则适用于第二个语句。我也想了解更多,但这不是我目前最关心的问题。
- 你为什么不尝试使用已经存在的文件或文件夹来重写你的 URLs?
因为没有。我正在使用 MVC 模式。
- 除了这些还有其他规则吗?
(已添加)
- 提及您的 index.php 和 htaccess 的路径以及它们在服务器中的位置。
(已添加)
- 尝试 1 @RavinderSingh13 的解决方案:
尝试: mysite.com/index.php
==> index.php 被访问(主页)
尝试: mysite.com/placeholdercateg/index.php
==> 404 未找到(问题 1 已解决)
尝试: mysite.com/anythingreally/reallyanything/etc/placeholdercateg/
==> placeholdercateg 页面被访问
根据您展示的尝试,示例;请尝试在您的规则文件中遵循 htaccess 规则。确保在测试您的 URL 之前清除您的浏览器缓存。
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=&page= [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=&action=&id=&postid= [NC,L]
##New rules for non-existing uris.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*/([\w-]+)/?$ /? [R=301,L]
RewriteRule ^(.*)/?$ index.php?param= [QSA,L]