otinanai
otinanai

Reputation: 4025

.htaccess redirect when specific path is found

I was struggling the last few days to write a proper solution for my .htaccess so that I can redirect (http://www.)domain.com/path/anotherpath to https://www2.domain.com/path/anotherpath.

I have managed to make it work as below:

RewriteCond %{THE_REQUEST} www\.domain\.com\path\anotherpath
RewriteRule (.*) https://www2.domain.com/path/anotherpath [R=301,L]

However, this solution doesn't seem to work when I need to redirect pages that belong to deeper levels like for example: domain.com/path/anotherpath/level1/level2 (level1 and level2 are dynamic). Now it redirects only when a specific full url is detected.

How can I make this work without defining the full urls in my htaccess?

Any help would be very much appreciated.

Thank you

Upvotes: 1

Views: 337

Answers (1)

anubhava
anubhava

Reputation: 785246

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^www\.(domain\.com)$ [NC]
RewriteRule ^(shop)(/.*|)$ http://www2.%1/$1$2 [R=301,L]

Upvotes: 1

Related Questions