htaccess rewrite rule, old URL to new

此生再无相见时 提交于 2020-02-23 08:39:08

问题


A bit of help fellow SO people.

What I have at the moment (based on some code I used for a different type of URL).

I want the first URL to redirect to the second, with no query string included afterwards

This is what I have to so far.

RewriteRule ^(page.php?id=missionstatement+)/?$ http://example.com/why/mission-statement [R=301,L]
RewriteRule ^(page.php?id=ofsted+)/?$ http://example.com/how/ofsted-report [R=301,L]
RewriteRule ^(page.php?id=governingbody+)/?$ http://example.com/governors [R=301,L]

回答1:


Here is the rule (will redirect 1 URL):

RewriteCond %{QUERY_STRING} ^id=whatever
RewriteRule ^page\.php$ http://%{HTTP_HOST}/how/somehow? [R=301,L]
  1. This rule intended to be placed in .htaccess in website root folder. If placed elsewhere some small tweaking may be required.

  2. I have used %{HTTP_HOST} -- this will redirect to the same domain as requested URL. If domain name has to be different, replace it by exact domain name.

  3. The ? at the end of new URL will get rid of existing query string.




回答2:


Ahoy!

Give this a whirl:

#check mod_rewrite is enabled
<IfModule mod_rewrite.c>

#enable mod rewrite
RewriteEngine On

#set working directory
RewriteBase /

#force trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [R=301,L]

#bootstrap index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page.php\?id=(.*)$ http://www.willans.com/page.php/$1 [R=310,L]

#end mod rewrite check
</IfModule>

It's been a while since i've done any web dev, but that should be a push in the right direction at least ;)



来源:https://stackoverflow.com/questions/7594697/htaccess-rewrite-rule-old-url-to-new

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!