我在htaccess文件中尝试执行301重定向时遇到问题。使用我创建的部分重写规则时遇到问题。总之,重写将页面的扩展名剥离,并将查询?primary-view = city替换为/ city。这是我的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]
我是否可以通过某种方式重写我的重写规则,或者是否需要使用重写条件来防止这种情况发生?
答案 0 :(得分:0)
这可能是因为您正在将mod_alias指令与mod_rewrite指令混合使用。这意味着两个指令以不需要的方式应用于同一请求。删除Redirect
指令(mod_alias)并改为使用重写规则。但您需要在其他规则之前添加规则(即在RewriteEngine On
之后)
RewriteRule ^chicago-downtown(.*)$ /chicago$1 [L,R=301]
答案 1 :(得分:0)
您不需要Redirect
规则。只需mod_rewrite
的其他chicago-downtown => chicago
规则:
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]