Willans
Willans

Reputation: 143

htaccess rewrite rule, old URL to new

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]

Upvotes: 0

Views: 1363

Answers (2)

LazyOne
LazyOne

Reputation: 165148

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.

Upvotes: 2

Zack Brown
Zack Brown

Reputation: 6028

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 ;)

Upvotes: 0

Related Questions