Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mod_rewrite rule to replace a sub-string from a URL

Tags:

mod-rewrite

I need to do this:

I have an old URL:

http://www.mysite.com/dev-site/some-content-part-here

That I want to make into:

http://www.mysite.com/live/some-content-part-here

Edit

I found somebody voted down on my question. I dont know what thought drove him/her to that but actually the question really was not that silly! May be the downer did not understand why I asked this... it could be anything in his mind. But I want all readers, if you dont like question - please always try to communicate through comments and then if the question poster does not agree your changes, then down vote. Thanks.

*And guys, thanks for editing.

like image 985
KMX Avatar asked Feb 24 '12 21:02

KMX


People also ask

What is $1 in Apache rewrite rule?

The $1 is basically the captured contents of everything from the start and the end of the string. In other words, $1 = (. *) .

How do I rewrite an Apache rule?

A rewrite rule can be invoked in httpd. conf or in . htaccess . The path generated by a rewrite rule can include a query string, or can lead to internal sub-processing, external request redirection, or internal proxy throughput.

What does a2enmod rewrite do?

a2enmod = Apache2 Enabled ModThe a2enmod command actually moves the Apache module files from /etc/apache2/mods-available to /etc/apache2/mods-enabled. The second method is to manually move the module files ourselves.


1 Answers

Untested:

RewriteEngine On

RewriteRule ^/dev-site/(.*)$ http://www.mysite.com/live/$1 [R=301,L]

Note that it might work if you only use /live/$1 on the right hand side, but I'd do this to be sure.

I know you know this (since you've mentioned mod_rewrite), but for those that don't, you generally put this into the .htaccess file in the directory - either dev-site or root should work.

like image 154
Tim Čas Avatar answered Oct 01 '22 00:10

Tim Čas