I'm trying to redirect one URL to another URL. They are different domains, but both have query strings in their URLs. The sample URLs are:
Redirect:
olddomain/faculty/profile?uID=123
to:
newdomain/Faculty?id=024
This is what I've placed in the .htaccess file and it does not work (all modules necessary are installed and activated as there are other 301's without query strings that work without issue):
RewriteCond %{HTTP_HOST} ^www\.olddomain\.tld$ [NC]
RewriteCond %{QUERY_STRING} ^uID=123$ [NC]
RewriteRule ^faculty/profile$ http://newdomain/Faculty?id=024 [R=301,NE,NC,L]
What am I doing wrong?
You need to add http:// also:
RewriteCond %{HTTP_HOST} ^www\.olddomain\.tld$ [NC]
RewriteCond %{QUERY_STRING} ^uID=123$ [NC]
RewriteRule ^faculty/profile$ http://newdomain/Faculty?id=024 [R=301,NE,NC,L]
And make sure this rule is placed at top just below RewriteEngine On.
Related
I am trying to redirect all URLs on my site (let's call it www.site1.com) except one in my .htaccess file. I currently have the following in my .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/my/page$ [NC]
RewriteRule (.*) https://www.site2.com [R=302,L]
With the above, all requests to www.site1.com are redirected to www.site2.com, including the one that I do not want to redirect.
After some experimentation, I have found that the following works to redirect only a specific page:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/my/page$ [NC]
RewriteRule (.*) https://www.site2.com [R=302,L]
I'm not sure why the ! operator isn't working as I expect. Perhaps there is an error in my regex?
Some additional bit of information. This is a Drupal site running on a dedicated cPanel host. But I have been sure to put this new redirect rewrite rule before all the other Drupal-specific rewrite rules.
Any help would be greatly appreciated.
RewriteCond %{REQUEST_URI} !/(index.php) [NC]
RewriteRule ^(.*)$ index.php [NC,QSA,L,END]
You need to make sure you exclude the page you redirect to from you list of possible url that get redirected(to avoid the loop) and then you can add more before the RewriteRule.
RewriteCond %{REQUEST_URI} !/(index.php) [NC]
RewriteCond %{REQUEST_URI} !/(index_new.php) [NC]
RewriteRule ^(.*)$ index.php [NC,QSA,L,END]
I need a hand with creating a regex rewrite rule for my .htaccess.
Here's the problem. My previous URLs structure was:
http://example.com/whatever-the-url-is/?lang=en
now I turned it into
http://example.com/en/whatever-the-url-is/
I'd like to create an .htaccess URL that 301 redirects all the URLs from the previous structure to the new one.
Also the .htaccess is shared between different domains, and so there should be
RewriteCond %{http_host} !^example.com$ [NC] condition at the beginning...
Is it possible? Do you have any suggestions?
You can use:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^example.com$ [NC]
RewriteCond %{QUERY_STRING} (?:^|&)lang=(.+?)(?:&|$) [NC]
RewriteRule ^(.*)$ /%1/$1? [R=301,L]
You can add the RewriteRule after RewriteCond %{HTTP_HOST}:
RewriteCond %{REQUEST_URI} !^/(?:en|fr|sp)/ [NC]
Where you test if langcode is already in the url.
I'm trying to redirect all urls from one domain to another but one (kind of). This is the htaccess I have to redirect all keeping the same url except of the domain (for example domain.com/something goes to domain2.com/something).
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule ^(.*)$ "http://domain2.com/$1" [R=301,L]
What I want to know is how to redirect all except if the url is domain.com/validation/*
/validation/ is not a subfolder and it has to be the next part of the url after the domain (domain.com/something/validation can redirect, and domain.com/validation/something can't).
I tried a lot of options but none worked :(
I hope is enough information.
You can exclude it in RewriteRule itself:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule !^validation http://domain2.com%{REQUEST_URI} [NE,NC,R=301,L]
I need to redirect from this URL:
www.devsite.com/level1/page1.html?brand=6
to:
www.productionsite.com/level1/page1.html?brand=6
I've come across various redirect w/ parameters answers here on stack, but none that specifically address how to rewrite part of .htaccess to redirect to a totally different domain. Help!
Just do a redirect from devsite to productionsite. Second line appends after the devsite domain to the new location.
RewriteCond %{HTTP_HOST} !^www.devsite.com$ [NC]
RewriteRule ^(.*)$ http://www.productionsite.com/$1 [L,R=301]
Add this to the htaccess file in the document root of your www.devsite.com domain. Add it above any other rules that may already be in that file.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?devsite\.com$ [NC]
RewriteRule ^level1/page1\.html$ http://www.productionsite.com/level1/page1.html [L,QSA]
RewriteCond %{QUERY_STRING} \?brand=6
RewriteCond %{HTTP_HOST} ^www\.devsite\.com$
RewriteCond %{SERVER_PORT}s ^(443(s)|\d+s)$
RewriteRule ^level1/page1\.html$ http%2://www.productionsite.com/$0 [R=301,QSA,L]
We have a multi-subdomain site that generates dynamic cotent depending on the subdomain text. However it does not work if www is appended to the subdomain. As some users are used to add www in front of every URL, we would like to fix it with a URL rewrite.
EDIT
I have got this far:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.subdominio\.dev [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{http_host} ^www\.([^\.]+)\.subdominio\.dev [NC]
RewriteRule ^(.*) http://%1.subdominio.dev$1 [R=301,QSA,NC]
Surprisingly. It works well in one of my test subdomains, but not in the other:
www.otro-mas.subdominio.dev gets redirected to otro-mas.subdomino.dev (with and without an URI like /index.html). Just as expected.
However www.ono.subdominio.dev is going into an infinite redirect. Like this:
http://www.ono.subdominio.dev/ono.subdominio.dev//ono.subdominio.dev//ono...
Why is it not rewriting the host?
Try this:
RewriteCond %{HTTP_HOST} ^www\.([a-zA-Z-_]+)\.domain\.com [NC]
RewriteRule ^(.*)$ http://%1\.domain\.com/$1 [R=301,NC,QSA,L]
You probably forgot the QSA and the L directives (search on the Apache mod_rewrite documentation for the explanation).
It does work.
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.subdominio\.dev [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{http_host} ^www\.([^\.]+)\.subdominio\.dev [NC]
RewriteRule ^(.*) http://%1.subdominio.dev$1 [R=301,QSA,NC]
I had some problem with the caches in my browser.