Apache .htaccess重写规则,友好URL+重定向

Apache .htaccess重写规则,友好URL+重定向,apache,.htaccess,mod-rewrite,redirect,url-rewriting,Apache,.htaccess,Mod Rewrite,Redirect,Url Rewriting,我正在使用.htaccess获取如下URL: /index.php?page=2 RewriteRule ^contact-us$ /index.php?page=2 [L] 并将其改写为,例如: /contact-us 我想做两件事: 加载/联系我们时,显示page/index.php?page=2,但保留友好的URL。我知道这看起来像: RewriteRule ^contact-us$ "index\.php\?page\=2" [L] 哪个行。但现在我也希望导航到/index.ph

我正在使用.htaccess获取如下URL:

/index.php?page=2
RewriteRule ^contact-us$ /index.php?page=2 [L]
并将其改写为,例如:

/contact-us
我想做两件事:

加载/联系我们时,显示page/index.php?page=2,但保留友好的URL。我知道这看起来像:

RewriteRule ^contact-us$ "index\.php\?page\=2" [L]

哪个行。但现在我也希望导航到/index.php?page=2的用户最终与我们联系-如何将301重定向与友好的URL重写相结合来实现这一点?

不确定为什么在重写规则的目标部分使用引号和转义符。目标部分不是正则表达式。这看起来很简单:

/index.php?page=2
RewriteRule ^contact-us$ /index.php?page=2 [L]
要重定向index.php?page=2,需要执行以下操作。此规则必须在上述规则之前,否则将进入重写循环

RewriteCond %{REQUEST_URI}  ^/index\.php$
RewriteCond %{QUERY_STRING} ^page=2$
RewriteRule .* /contact-us [R=301,L]

这里您设置了两个重写条件:页面为/index.php,查询字符串为page=2且仅为page=2。R=301标志将强制对/联系我们进行外部重写,并发送和HTTP 301头。

您可以在文档\u ROOT/.htaccess文件中使用此代码:


RewriteRule^index\.php?page=12$/戒毒康复计划[R=301,L]RewriteRule^戒毒康复计划$index.php?page=12[L]这看起来正确吗?@SarahBee查看我的最新更新。我最初没有正确地显示重写规则。RewriteCond%{REQUEST_URI}^/index\.php$RewriteCond%{QUERY_STRING}^page=12$RewriteRule.*/戒毒康复程序[R=301,L]RewriteCond%{HTTP_HOST}^edgewood.ca$[或]RewriteCond%{HTTP_HOST}^www.edgewood.ca$RewriteRule^戒毒康复计划$index.php?page=12[L]抱歉,我的小减价格式设置有点不正确。我现在有了这个和重定向循环。此外,它还使用查询字符串/戒毒治疗恢复程序重定向?page=12确定,我知道我必须添加问号以阻止查询字符串,但仍然不确定如何避免此重定向循环。@SarahBee您应该使用最新代码更新OP。很难说你在做什么。我无法从您所展示的内容看出什么会导致重定向循环。我不知道为什么第二条重写规则会有这些重写条件。