I have the following structure in my htaccess file. My issue is if URI matches with condition URL redirect according to rule and if URI doesn't match default rule should apply, please tell me how to fix.
First Rule: RedirectMatch 301 /mob/(.*) http://www.newdomain.com/price/$1
Default Rule: RedirectMatch 301 /(.*) http://www.newdomain.com/$1
when I tried this default rule overriding other rules.
Assuming both domains are on the same root folder and host:
RewriteCond %{HTTP_HOST} ^originaldomain\.com$
RewriteCond %{REQUEST_URI} ^/cms
RewriteRule ^(.*)$ https://differentdomain.com/$1 [L,R=302]
If they are not on the same root and folder:
RewriteCond %{REQUEST_URI} ^/cms
RewriteRule ^(.*)$ https://differentdomain.com/$1 [L,R=302]
Now the 2nd part if the url is not a act, url, system or post:
RewriteCond %{THE_REQUEST} !^[A-Z]{3,}\s/(act\?(.*)|url|system)$ [NC]
RewriteCond %{THE_REQUEST} !^POST [NC]
RewriteRule ^(.*)$ http://originaldomain.com/$1 [L,R=302]
Basically this should work, if it does after you test change to 302 to 301 if needed.
Related
I saw similar topics but couldn't find a practical answer to my problem.
I'm moving my old website to a new one, and some URLs are changing.
I would like to make a generic 301 redirection to the new domain (because most paths are the same), while individually redirecting some URLs.
Here is what I have on my old website .htaccess :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^old\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.old\.com$
RewriteRule (.*)$ https://new.com/$1 [R=301,L]
Redirect 301 "/custom/url/" "https://new.com/my-custom-url"
</IfModule>
But the 301 redirects to : https://new.com/custom/url instead of https://new.com/my-custom-url
Some of my URLs also have URL parameters I would like to redirect, such as :
Redirect 301 "/brand.php?name=Example" "https://new.com/Example"
Redirect 301 "/brand.php?name=Example2" "https://new.com/another/url"
which do not seem to work as well.
Thank you very much for your help.
But the 301 redirects to : https://new.com/custom/url instead of https://new.com/my-custom-url
It is because your specific redirect rule appears after generic one. Moreover you are mixing mod_rewrite rules with mod_alias rules and these are invoked at different times.
Have it like this:
RewriteEngine On
# redirect /brand.php?name=Example2 to new.com/another/Example2
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteCond %{QUERY_STRING} ^name=(Example2) [NC]
RewriteRule ^brand\.php$ https://new.com/another/%1? [R=301,L,NE]
# redirect /brand.php?name=Example3 to new.com/category/Example3
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteCond %{QUERY_STRING} ^name=(Example3) [NC]
RewriteRule ^brand\.php$ https://new.com/category/%1? [R=301,L,NE]
# generic redirect /brand.php?name=Example to new.com/Example2
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteCond %{QUERY_STRING} ^name=([^&]+) [NC]
RewriteRule ^brand\.php$ https://new.com/%1? [R=301,L,NE]
# redirect custom URL
RewriteRule ^custom/url/ https://new.com/my-custom-url [R=301,L,NE,NC]
# redirect everything else
RewriteCond %{HTTP_HOST} ^(www\.)?old\.com$ [NC]
RewriteRule ^ https://new.com%{REQUEST_URI} [R=301,L]
Following works fine
RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
Now I need to do the same but with a directory, like
www.mydomain.com/oldname/something to www.mydomain.com/NEWNAME/something
Before the rule that redirects the domain, add these rules:
RewriteRule ^/?oldname/(.*)$ http://www.mydomain.com/newname/$1 [L,R=301]
I am trying to rewrite my .htaccess file to redirect old dynamic URLS to new ones.
I have tried these so far but they are not working as expected. The second rule seems to redirect to the first one.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain.com$
RewriteRule ^(.*)$ "http\:\/\/www\.mydomain\.com\/$1" [R=301,L]
Redirect 301 /product.php?l=old-product-name1 http://www.mydomain.com/product.php?l=new-product-name1
Redirect 301 /product.php?l=old-product-name2 http://www.mydomain.com/product.php?l=new-product-name2
Can anyone help me redirect these properly?
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain.com$
RewriteRule ^(.*)$ "http\:\/\/www\.mydomain\.com\/$1" [R=301,L]
RewriteCond %{QUERY_STRING} =l=old-product-name1
RewriteRule ^product.php$ http://www.mydomain.com/product.php?l=new-product-name1 [R=301]
I've been struggling to do 301 redirects, and none of the existing topics helps.
I need to make redirects with .htaccess for the following pages:
Redirect 301 http://www.mypage.com/?q=company/contacts http://www.mypage.com/contacts
Redirect 301 http://www.mypage.com/?q=product/new/ghz/name-5 http://www.mypage.com/name-5
I know that I should use rewrite rules and specify {QUERY-STRING} and believe me, I've tried. Nothing helps.
Matching against the actual request:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?q=company/contacts
RewriteRule ^$ http://www.mypage.com/contacts? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?q=product/new/ghz/name-5
RewriteRule ^$ http://www.mypage.com/name-5? [R=301,L]
Or the query string (this matches rewritten URIs)
RewriteCond %{QUERY_STRING} ^q=company/contacts$
RewriteRule ^$ http://www.mypage.com/contacts? [R=301,L]
RewriteCond %{QUERY_STRING} ^q=product/new/ghz/name-5$
RewriteRule ^$ http://www.mypage.com/name-5? [R=301,L]
I'm trying to accomplish the following:
http:// www.example.com/site/abc with a http 301 redirect to subdomain http:// abc.example.com
and back again within Apache:
http:// abc.example.com --> /site/abc
I want both redirects to be defined in the .htaccess in the root folder.
I've tried several combinations, but unfortunately without any luck. This is what I have now:
# 1. redirect uris which start with www. to the domain without www.
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com$
RewriteRule ^(.*)$ http://%1.example.com/$1 [R=301,L]
# 2. rewrite http://host/site/<name>/<uri> => http://<name>.host/<uri>
RewriteCond %{HTTP_HOST} ^example\.com
RewriteCond %{REQUEST_URI} ^/site/([^/]+)
RewriteRule ^(.*) http://%1.example.com/$1 [R=301,NC,L]
# 3. internal redirect to the corresponding directory
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^(.*)$ site/%1/ [L,NC]
I receive a 500 server error instead.
Thanks in advance!
Just to clarify in your original question you said you need this redirection:
http://www.example.com/site/abc => http://abc.example.com/site/abc (**site/abc also present** in destination URL)
But later in your comment you suggested:
http://www.example.com/site/abc/xyz/part?id=123&name=lmn => http://abc.example.com/xyz/part?id=123&name=lmn (**site/abc missing** from destination URL)
Assuming your comment are right, please try this in your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule site/(.+)/(.*) http://$1.example.com/$2 [R=301,L]
This will redirect www.example.com/site/foo/bar* to foo.example.com/bar* with 301 status to the browser.
Assuming /site/abc/xyz/part is a actual physical file on disk try following (if actual file got some extension then append it).
Also add QSA flag so that query string is appended.
# 1. redirect uris which start with www. to the domain without www.
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.example\.com$
RewriteRule ^(.*)$ http://%1.example.com/$1 [R=301,L,QSA]
# 2. rewrite http://host/site/<name>/<uri> => http://<name>.host/<uri>
RewriteCond %{HTTP_HOST} ^example\.com
RewriteCond %{REQUEST_URI} ^/site/([^/]+)
RewriteRule ^(.*) http://%1.example.com/$1 [R=301,NC,L,QSA]
# 3. internal redirect to the corresponding directory
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^(.*)$ site/%1/ [L,NC,QSA]