dcp3450
dcp3450

Reputation: 11187

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?

Upvotes: 12

Views: 22559

Answers (1)

Jon Lin
Jon Lin

Reputation: 143916

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.

Upvotes: 23

Related Questions