dhrm
dhrm

Reputation: 14944

htaccess redirect specific subfolder

I'm would like to setup a htaccess HTTP 301 redirect from /abc/* to http://newdomain.com/xyz/*.

I'm trying with this rewrite rule, but it is not working as expected:

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/abc/(.*)?$ [NC]
RewriteRule ^ http://newdomain.com/xyz%{REQUEST_URI} [R=301,L]
</IfModule>

It redirect /abc/test to http://newdomain.com/xyz/abc/test. How can I change the rewrite rule to remove the abc part from the request uri?

Upvotes: 0

Views: 22

Answers (1)

Mohammed Elhag
Mohammed Elhag

Reputation: 4302

Change this line :

RewriteRule ^ http://newdomain.com/xyz%{REQUEST_URI} [R=301,L]

to :

RewriteRule ^ http://newdomain.com/xyz/%1 [R=301,L]

Bacuse the %{REQUEST_URI} in the first line including /abc/ so you should seperate it from URI .

your all rules should look like this :

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/abc/(.*)?$ [NC]
RewriteRule ^ http://newdomain.com/xyz/%1 [R=301,L]
</IfModule>

So , %1 represent this (.*)? after /abc/ in RewriteCond

You could also summerize this rule like the folwoing :

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^abc/(.*)$ http://newdomain.com/xyz/$1 [R=301,L]
</IfModule>

Note: clear browser cache the test

Upvotes: 1

Related Questions