.htaccess rewrite url [apache]


.htaccess rewrite url [apache]

我想用.htaccess重写一个url,并对其进行301重定向。

原始url是这样的http://localhost/labs/customer_132/?kund=goteborg-stad

我希望它看起来像这样,并将其重定向到

http://localhost/labs/customer_132/?page_id=11#goteborg-stad

尝试了不同的解决方案,但我不掌握窍门。

感谢您的帮助

不能使用RewriteRule雄蕊直接访问查询字符串;相反,您需要一个前面的RewriteCond,它检查是否有kund字段并捕获其值。然后,您可以在RewriteRule中使用%N来使用任何捕获的值。

RewriteEngine On
RewriteCond %{QUERY_STRING} kund=(['w'-]+)
RewriteRule ^/labs/customer_132/ /labs/customer_132/?page_id=11#%1 [NC,R,L]

您需要打开.htaccess文件中的"重写"引擎:

RewriteEngine On

然后,对于单个URL重定向,以下操作应该有效(如果.htaccess文件在根文件夹或服务器中)。

编辑:您需要检查查询字符串条件,如果查询字符串恰好是kund=goteborg-stad(大写或小写),则此条件将匹配。

RewriteCond %{QUERY_STRING} ^kund=goteborg-stad$ [NC]
RewriteRule ^labs'/customer_132'/$ /labs/customer_132/?page_id=11#goteborg-stad [NE,R,L]

NE告诉Apache不要对重定向中的#符号进行转义。

如果你想重定向选择的不同URL(例如,客户ID会改变吗?),那么你需要澄清。