htaccess奇怪的恢复与url重写规则和301重定向一起发生



我在尝试在htaccess文件中执行301重定向时遇到问题。它在处理我创建的重写规则的一部分时遇到了问题。总之,重写从页面上删除了扩展名并替换了查询?主视图=城市到/城市。这是我的htaccess文件。

RewriteEngine On
# Redirect /page.php to /page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s/+(page).php [NC]
RewriteRule ^ /%1 [R=302,L]
# Internally forward /page to /page.php
RewriteRule ^(page)/?$ /$1.php [L,QSA,NC]
# Redirect /page.php?primary-view=value to /page/value
RewriteCond %{THE_REQUEST} s/+(page).php?primary-view=([^s]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]
# Internally forward /page/value to /page.php?primary-view=value
RewriteRule ^(page)/(.*)/?$ /$1.php?primary-view=$2 [L,QSA,NC]
# Redirect /chicago-downtown to /chicago
Redirect 301 /chicago-downtown to /chicago

所以重写后的url看起来像

#Original url
example.com/page.php?primary-view=chicago-downtown
#Url rewritten properly
example.com/page/chicago

如果我输入example.com/page/chicago,它就可以正常工作。如果我在example.com/page/chicago-downtown中键入它当前重定向到以下位置:

#Current bad redirect from example.com/page/chicago-downtown
example.com/page/chicago?primary-view=chicago-downtown
#Desired redirect from example.com/page/chicago-downtown
example.com/page/chicago

我尝试过人工操作,得出的结论是,301重定向的问题是由以下几行引起的。

# Internally forward /page/value to /page.php?primary-view=value
RewriteRule ^(page)/(.*)/?$ /$1.php?primary-view=$2 [L,QSA,NC]

有没有什么方法可以重写我的重写规则,或者我需要使用重写条件来防止这种情况发生?

您不需要Redirect规则。只是chicago-downtown => chicago的附加mod_rewrite规则:

RewriteEngine On
RewriteRule ^(page/chicago)-downtown/?$ /$1 [L,R=302,NC]
# Redirect /page.php to /page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}s/+(page).php [NC]
RewriteRule ^ /%1 [R=302,L]
# Internally forward /page to /page.php
RewriteRule ^(page)/?$ /$1.php [L,QSA,NC]
# Redirect /page.php?primary-view=value to /page/value
RewriteCond %{THE_REQUEST} s/+(page).php?primary-view=([^s]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]
# Internally forward /page/value to /page.php?primary-view=value
RewriteRule ^(page)/(.*)/?$ /$1.php?primary-view=$2 [L,QSA,NC]

这可能是因为您将mod_alias指令与mod_rewrite指令混合在一起。这意味着两个指令都以一种不需要的方式应用于同一个请求。删除Redirect指令(mod_alias)并使用重写规则。但您需要在您的其他规则之前(即RewriteEngine On之后)添加规则

RewriteRule ^chicago-downtown(.*)$ /chicago$1 [L,R=301]

相关内容

  • 没有找到相关文章