Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use an .htaccess redirect for a partial path?

I had to adjust some paths on my site and I need to use .htaccess to redirect the items on the chance a user accesses the old url.

For example my old urls (relative) could be:

/old-path/page1.php
/old-path/page2.php
/old-path/page3.php
etc...

I had to change the path (for this example) to new-path and I need to adjust the .htaccess so anyone that comes to any page with .../old-path/... will be redirected to

.../new-path/...

Also, would this satisfy the 301 or would I need to list out each page?

like image 919
dcp3450 Avatar asked Sep 24 '12 15:09

dcp3450


1 Answers

You can use either mod+alias:

Redirect 301 /old-path /new-path

or using mod_rewrite:

RewriteEngine On
RewriteRule ^/?old-path/(.*)$ /new-path/$1 [L,R=301]

These could be in the htaccess file in your document root or in the server/vhost config. If you already have rewrite rules somewhere, you may just want to stick with mod_rewrite because redirecting with mod_alias while using mod_rewrite can sometimes give conflicting results.

like image 151
Jon Lin Avatar answered Oct 29 '22 01:10

Jon Lin