boruchsiper
boruchsiper

Reputation: 2028

.htaccess replace parts of the url

I want the htaccess Redirect 301 to do the following:

http://mysite.com/article.php?id=123

to

http://mysite.com/123

Another words, to remove the "article.php?id"part

Any help would be much appreciated.

Upvotes: 1

Views: 513

Answers (2)

anubhava
anubhava

Reputation: 785266

Use this code in your .htaccess file:

Options +FollowSymlinks -MultiViews
RewriteEngine On

RewriteCond %{QUERY_STRING} ^id=([^&]*)(&|$) [NC]
RewriteRule ^article\.php$ /%1? [L,R=301,NC]

It is important to use ? in the end to get rid of original query string.

Upvotes: 2

Floern
Floern

Reputation: 33904

You can do this with mod_rewrite if it's enabled:

RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^article\.php$ http://%{HTTP_HOST}/%1 [L,R=301]

Upvotes: 1

Related Questions