How to rewrite rules in htacess file - regex

I want to write rules to redirect to following pages using .htaccess file.I am new to this.
How I suppose to do it?
Facebook: test.com/FB (redirects to https://www.facebook.com/test.etutors)
Twitter: test.com/Twitter (redirects to https://twitter.com/test)

Your attempted rule won't work because RewriteRule only matches request URI not the domain part. Have your rules like this in your root .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)test\.com$ [NC]
RewriteRule ^FB/?$ https://www.facebook.com/test.etutors [L,NC,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)test\.com$ [NC]
RewriteRule ^Twitter/?$ https://twitter.com/test [L,NC,R=301]
PS: Since you're a new user I suggest you to show your code by editing your question rather than in comments.

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.

improvement of a mod_rewrite rule

Could you have a look at the code below and say if this is the correct way to do it please.
In order to redirect from:
http://blog.exampledomain.com/2013/10/testpage.html
to
https: //exampledomain.com/blog/testpage/
I have placed the below rewrite rules in 2 .htaccess files:
In the .htaccess file in root of exampledomain.com I have placed:
RewriteCond %{HTTP_HOST} ^blog\.exampledomain\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.blog\.exampledomain\.com$ [NC]
RewriteRule ^(.*)$ https://exampledomain.com/blog/$1 [R=301,L]
In the .htaccess file in the blog folder in exampledomain.com/blog I have placed:
RewriteRule ^([0-9]{4})/([0-9]{2})/(.*)\.html https://exampledomain.com/blog/$3/ [R=301,L]
Questions:
Is this the correct way to do it?
Does this mean in this setup we have 2 redirects? (which is probably not good for SEO) Should these be combined? How?
These two redirects should be combined into one.
In the .htaccess file in the document root of your subdomain (which looks it might be the same as the document root of the main domain? Or in the main domains .htaccess file - if this is a parent directory on the filesystem), try the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?blog\.exampledomain\.com$
RewriteRule ^\d{4}/\d\d/(.+)\.html$ https://exampledomain.com/blog/$1/ [R,L]
When you are sure this is working OK then change the temporary redirect (R) into a permanent one (R=301).
UPDATE: To also redirect from: http://blog.exampledomain.com/ to https://exampledomain.com/blog/ and http://blog.exampledomain.com/whatever to https://exampledomain.com/blog/whatever, add the following directives after the above:
RewriteCond %{HTTP_HOST} ^(www\.)?blog\.exampledomain\.com$
RewriteRule ^(.*)$ https://exampledomain.com/blog/$1 [R,L]

htaccess redirect files to new domain

I need to direct all files on one domain to the homepage of the new domain (I have to due to a new structure). I seem unable to do this, so for example I use the below code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^olddomain\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://www.newdomain.co.uk [R=301,L]
But if I have the below link:
www.olddomain.co.uk/shop/product_info.php?cPath=592&products_id=9335 it does not direct as I want it to, I want it to direct to the homepage of www.newdomain.co.uk but it goes to:
http://www.newdomain.co.uk/?cPath=592&products_id=9335
What do I need to do to amend the htacess on the old domain?
Thanks
You can use this rule:
RewriteCond %{QUERY_STRING} ^cpath= [NC]
RewriteRule ^/?$ /? [R=301,L]
/? in target will strip off original query string.

.htaccess redirect with parameters and multiple directory levels

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]