Htaccess - Redirect all but one url - regex

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]

Related

Redirect all urls except one in .htaccess

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]

301 redirect.htaccess on entire site EXCEPT for main domain url

I all, I've been searching a lot but I don't find an answer to my problem.
I have already made a .htaccess file that redirects all the different pages and files on my old domain (www.olddomain.com) to my new domain (www.newdomain/blog.com). I'm moving a blog to an e-commerce, that's why 301 to www.newdomain/blog.com.
However, I need to use the Change of Address Tool (Google Webmaster Tools) but I can't because www.olddomain.com must redirect to www.newdomain.com and now it is redirecting to www.newdomain/blog.com and Google don't let me change the address.
My .htaccess is working fine except for that, here it is
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule (.*)$ http:/www.newdomain.com/blog/$1 [R=301,L]
BUT, I have not found any way to make an exception to redirect just www.olddomain.com must ---> www.newdomain.com
I have tried to make that exception in the following way:
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule ^/$ http://www.newdomain.com/ [R=301,L]
RewriteRule (.*)$ http://www.newdomain.com/blog/$1 [R=301,L]
But nothing has worked...
Please, can you help me?
Thanks in advance
Try :
RewriteEngine On
#redirect http://oldsite.com/ to http://newsite.com
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$
RewriteRule ^$ http:/www.newdomain.com/$1 [R=301,L]
#redirect http://oldsite.com/pages to http://newsite.com/blog/pages
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$
RewriteRule ^(.+)$ http:/www.newdomain.com/blog/$1 [R=301,L]
Note the ^$ in pattern it matches / ie : http://example.com/ or the directory the htaccess file is in.
Clear your browser cache before testing this.

mod_rewrite for complex subdomain redirects

These are my current directives for redirecting subdomain HTTP requests to a domain with a path.
RewriteCond %{HTTP_HOST} ^blog.website.com
RewriteRule ^(.*)$ http://website.com/blog [L]
However, this site is a new version of a website for which social media links have been posted. These links have image URLs which are now broken since the URL now points to a different server. The images have since been migrated and the path to said images has been preserved on the new server. In light of this, I need rewrite my directives to account for these legacy links.
I need to write conditions that rewrite the URL only when the URL no path or when the first parameter of the URL is not /wp-content/.
I have to make conditions and rules that differentiate between these 3 URL patterns:
http://blog.website.com
-> http://website.com/blog
http://blog.website.com/wp-content/year/month/image.jpg
-> http://website.com/wp-content/year/month/image.jpg
http://blog.website.com/entry-url-title
-> http://website.com/blog/entry-url-title
This is ultimately how I was able to do it, with a little help from my friends, of course.
RewriteCond %{HTTP_HOST} blog.website.com
RewriteCond %{REQUEST_URI} ^/?$
RewriteRule ^(.*) http://website.com/blog [R=301,L]
RewriteCond %{HTTP_HOST} ^blog.website.com
RewriteCond %{REQUEST_URI} !wp-content
RewriteRule ^(.*) http://website.com/blog/%{REQUEST_URI} [R=301,NC,L]
RewriteCond %{HTTP_HOST} ^blog.website.com
RewriteCond %{REQUEST_URI} wp-content
RewriteRule ^(.*) http://website.com/%{REQUEST_URI} [R=301,NC]

Htaccess, How do I redirect this dynamic URL

I have a strange redirect situation that I cannot get to work.
www.example.com/?NR
www.example.com/?PV
I need to get these two URL's rewritten and redirected to the home page but I can't get it to work. Here is what I have tried:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^NR$ [NC]
RewriteRule www.example.com/ http://www.example.com [R=301,L]
RewriteCond %{QUERY_STRING} ^PV$ [NC]
RewriteRule www.example.com/ http://www.example.com [R=301,L]
We bought this site and I have no reference to what those pages might actually be, they just need to be redirected and rewritten properly.
This one rule should work for both of your requirements:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(NR|PC)$ [NC]
RewriteRule ^/?$ /? [R=301,L]
Remember that RewriteRule only matches REQUEST_URI which is URI part without domain name and query string.
/ in target URI is for your home URL and ? in the end is to strip any existing query string.
Reference: Apache mod_rewrite Introduction

mod_rewrite .htaccess subdomain redirect

I spend the last 2 days searching and learning about .htaccess to redirect all subdomains to a user folder
without changing the url, but i keep having 3 little problems. I really would appreciate some help
problem 1)
If i go to http://www.example.com/users/foo it automatically redirect
the url to http://foo.example.com/ but if i go to http://www.example.com/users/foo/dashboard.php
it does nothing, instead of redirecting to http://foo.example.com/dashboard.php
this is the code i use:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /users/(.+)/\ HTTP/
RewriteRule ^users/([^/]+)/(.*)$ http://$1.example.com/$2 [R=301,L]
problem 2)
if i go to http://foo.example.com/dashboard.php
it gets its information from http://www.example.com/users/foo/dashboard.php
but if i type foo.example.com in the address bar (in firefox) it (sometimes) automatically redirect
to www.foo.example.com, can i prevent the www prefix?
this is the code
RewriteCond %{REQUEST_URI} !^/users/
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteCond %{HTTP_HOST} ([^.]+)\.example\.com
RewriteRule (.*) /users/%1/$1 [L]
problem 3)
the campaign website is locaded in the folder http://www.example.com/website but should be
visible on the url http://www.example.com/ (like i get the information from http://www.example.com/users/foo
on the url http://foo.example.com/).
About problem one I would just check the host:
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule ^users/([^/]+)/(.*)$ http://$1.domain.com/$2 [R=301,L]
About problem second this is a modified rule which I got from the drupal .htaccess file:
# remove www prefix
RewriteCond %{HTTP_HOST} ^www\.(.+)\.domain\.com$ [NC]
RewriteRule ^ http://%1%.domain.com{REQUEST_URI} [L,R=301]
# redirect to the subdirectory
RewriteCond %{HTTP_HOST} !^www\.(.+)\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$ [NC]
RewriteRule ^ user/%1%{REQUEST_URI} [L,R=301]
About your third problem check this one here:
RewriteCond %{HTTP_HOST} ^www.domain.com$
RewriteRule ^(.*)$ website/$1 [R=301,L]