[英]How to fix: RewriteRule not working in .htaccess
Please consider the content in my .htaccess
:请考虑我的
.htaccess
中的内容:
##
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
## Allow a few SEO Files direct access.
RewriteRule ^robots.txt?$ robots.txt [L]
RewriteRule ^ads.txt?$ ads.txt [L]
RewriteRule ^sellers.json?$ sellers.json [L]
## Avoid rewriting rules for the admin section
RewriteRule ^(admin|resources)($|/) - [L]
## Set Ajax Request File
RewriteRule ^(kahuk-ajax)/?$ kahuk-ajax.php? [L,QSA]
## Set controller with id
RewriteRule ^([^/]+)/([0-9]+)/?$ index.php?con=$1&id=$2 [L,QSA]
## Set controller with slug
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?con=$1&slug=$2 [L,QSA]
## For paging
RewriteRule ^([^/]+)/page/([0-9]+)/?$ index.php?con=$1&page=$2 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/page/([0-9]+)/?$ index.php?con=$1&slug=$2&page=$3 [L,QSA]
## Set controller for only one parameter
RewriteRule ^page/([^/]+)/?$ index.php?con=page&slug=$1 [L,QSA]
RewriteRule ^([^/]+)/?$ index.php?con=$1 [L,QSA]
## Set home page
RewriteRule ^/?$ index.php?con=home [L]
Whenever I try to browse http://example.com/kahuk-ajax/?prefix=manage-story-vote
, this opened the index.php
instead of kahuk-ajax.php
!每当我尝试浏览
http://example.com/kahuk-ajax/?prefix=manage-story-vote
时,都会打开index.php
而不是kahuk-ajax.php
!
What am I doing wrong?我究竟做错了什么?
## Set Ajax Request File RewriteRule ^(kahuk-ajax)/?$ kahuk-ajax.php? [L,QSA]: ## Set controller for only one parameter: RewriteRule ^([^/]+)/?$ index.php?con=$1 [L,QSA]
The request is first rewritten to kakuk-ajax.php
by the first rule, but the second to last rule then rewrites this to index.php
during the second pass by the rewrite engine.该请求首先被第一条规则重写为
kakuk-ajax.php
,但倒数第二条规则随后在重写引擎的第二次传递期间将其重写为index.php
。
You need to prevent that second rule from rewriting requests of the form kakuk-ajax.php
.您需要防止第二条规则重写
kakuk-ajax.php
形式的请求。 If these URLs do not contain dots in the first path segment (that ordinarily delimits the file extension) then you could simply include a dot in the negated character class. For example:如果这些 URL 在第一个路径段(通常分隔文件扩展名)中不包含点,那么您可以简单地在否定字符 class 中包含一个点。例如:
## Set controller for only one parameter
:
RewriteRule ^([^/.]+)/?$ index.php?con=$1 [L,QSA]
Alternatively, exclude specific file extensions:或者,排除特定的文件扩展名:
RewriteCond %{REQUEST_URI} \.(txt|json|php)$
RewriteRule ^([^/]+)/?$ index.php?con=$1 [L,QSA]
Or, any URL that looks like it has a file extension:或者,任何看起来像具有文件扩展名的 URL:
RewriteCond %{REQUEST_URI} \.\w{2,5}$
RewriteRule ^([^/]+)/?$ index.php?con=$1 [L,QSA]
Aside:在旁边:
RewriteRule ^(kahuk-ajax)/?$ kahuk-ajax.php? [L,QSA]
This rule is a little contradictory.这个规则有点矛盾。 You are removing the query string with the trailing
?
您要删除尾随
?
, but then appending it again with the QSA
flag. , 但随后再次附加
QSA
标志。 If you wish to preserve the query string then you don't need either.如果您希望保留查询字符串,则两者都不需要。 And since you are capturing the URL-path, you might as well make use of this in the substitution string.
由于您正在捕获 URL 路径,因此您不妨在替换字符串中使用它。 For example:
例如:
RewriteRule ^(kahuk-ajax)/?$ $1.php [L]
Also, be wary of allowing an optional trailing slash here.另外,请注意不要在此处使用可选的尾部斜杠。 This promotes duplicate content which could potentially cause SEO issues.
这会促进可能导致 SEO 问题的重复内容。 Consider redirecting one to the other instead.
考虑将一个重定向到另一个。
## Allow a few SEO Files direct access. RewriteRule ^robots.txt?$ robots.txt [L] RewriteRule ^ads.txt?$ ads.txt [L] RewriteRule ^sellers.json?$ sellers.json [L]
The ?
的
?
at the end of the regex makes the preceding character ( t
and n
in the above examples) optional, which does not really make sense here.在正则表达式的末尾使前面的字符(上面示例中的
t
和n
)可选,这在这里没有真正意义。 Also, you don't need to rewrite to the same file - you don't want any rewrite/substitution to occur here.此外,您不需要重写同一个文件——您不希望此处发生任何重写/替换。 So, the above is the same as:
所以,上面的是一样的:
RewriteRule ^(robots\.txt|ads\.txt|sellers.json)$ - [L]
The -
(hyphen) explicitly indicates "no substitution". -
(连字符)明确表示“无替代”。
However, having included a "dot" in the (negated character class) directive above, this directive is redundant, except to "fail early".然而,在上面的(否定字符类)指令中包含了一个“点”,这个指令是多余的,除了“提前失败”。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.