unable to come up with htaccess rewrite engine - regex

I want to redirect domain1.com to domain2.com in all cases except one particular case:
domain1.com/subfolder/index.php
I want this domain1.com/subfolder/index.php to be intact and not get redirected to domain2.com because I have hundreds of users already bookmarked this page.
But anything and everything besides that domain1.com/subfolder/index.php, I want domain1.com to be redirected to domain2.com
Please help.

Have the following .htaccess in your web root /
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)domain1.com$ [NC]
RewriteCond %{REQUEST_URI} !^/subfolder/index.php$ [NC]
RewriteRule ^(.*)$ http://domain2.com/$1 [R=301,L]
Use just [L] if you want the redirection to be transparent i.e. without letting your visitors know.
Use !^/subfolder/.*$ if you want to stop redirection for the complete folder as well as its contents.

You can use RewriteCond to check to see the requested uri is anything but the one you want to redirect, capture the desired path and then redirect to the second domain.
RewriteCond %{REQUEST_URI} !^/subfolder/index.php$
RewriteRule (.*) http://domain2.com/$1

Related

Redirect non-www to www giving me hundreds of 404's

Thanks to anyone who can take a moment to look at this.
Recently I created a new section "subdomain" in my website and in this new folder I have includes a Joomla CMS installation the url looks like this: http://www.example.com/subdomain/
In this folder I have a htaccess file to which I have added.
## No directory listings
# Redirect non-www to www:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
When I try to access say http://example.com/subdomain/anytrailingstring then it's NOT redirecting me to http://www.example.com/subdomain/anytrailingstring as I expected, it is redirecting to http://www.example.com/anytrailingstring leaving out the /subdomain/ and this is of course a page that doesnt exist and therefore a 404.
This is a problem.
I do not have any directive in the root .htacces file except for this :
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Can someone perhaps see why the subdomain htaccess isnt redirecting to correctly? Did I miss something?
I am not good with htaccess at all, if anybody can help me I would really appreciate it.
Thanks!
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
You need to use the REQUEST_URI server variable instead of the backreference ($1). The URL-path matched by the RewriteRule pattern (first argument) is relative to the current directory, so excludes the parent subdirectory (ie. /subdomain in your example).
Do it like this instead:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
You will need to clear your browser cache since the erroneous (301 - permanent) redirect will have been cached by the browser. Test with 302 (temporary) redirects to avoid potential caching issues.
However, a couple of questions:
Why are you not using HTTPS? (You are redirecting to HTTPS in the parent .htaccess file - but this is now being overridden by the mod_rewrite directives in the subdirectory.)
Why not include this in the parent .htaccess file?
UPDATE: So, taking the above points into consideration... if you want to move this rule to the parent .htaccess file in the root then have it like this:
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine on
# Redirect non-www to www (and HTTPS)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Redirect HTTP to HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The order of the directives is to ensure there is only ever at most 1 redirect (assuming you are not implementing HSTS).
You were unnecessarily duplicating the RewriteEngine directive (so I removed the second instance).
The RewriteBase directive was not being used.
The capturing subgroup in your HTTP to HTTPS rule was not required. ie. ^ is better than ^(.*)$ in this instance.
Aside:.
...a new section "subdomain" in my website and in this new folder I have includes a Joomla CMS installation the url looks like this: http://www.example.com/subdomain/
This is a subdirectory, not a "subdomain".
This is a "subdomain":
http://subdomain.example.com/

HTAccess Redirect specific URLs to new domain pages and then rest to Root

Having checked other questions and trying the suggested solutions, nothing has worked so far.
I'm trying to redirect certain URLs from the old-domain to URLs on the new-domain, not necessarily with the same page names. The rest of the URLs should be redirected to the root of the new-domain. This is what I've tried. The redirecting of all pages to the root of the new-domain works, just not the individual pages:
RewriteEngine on
Redirect 301 /travel/ferry.html http://www.new-domain.com/ferry/
RewriteEngine off
#
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?old-domain\.com$ [NC]
RewriteRule ^ http://www.new-domain.com/? [R=301,L]
Thank you.
Don't mix Redirect directive and RewriteRule directives as they come from different Apache modules and their order of execution might be unpredictable.
You may have your rules as this:
RewriteEngine on
# keep specific redirect here
RewriteRule ^travel/ferry\.html$ http://www.new-domain.com/ferry/ [L,NC,R=301]
# redirect rest of the URLs to root
RewriteCond %{HTTP_HOST} ^(?:www\.)?old-domain\.com$ [NC]
RewriteRule ^ http://www.new-domain.com/? [R=301,L]
Make sure to test it in a new browser or test after fully clearing browser cache.

Apache Redirect URI Requests

I'm trying to redirect http requests that contain a specific URI to a different domain with a different URI completely. Redirecting the top level domain works but I can't seem to get the URI rules to redirect.
In essence it should act as follows:
If the url request is:
www.example.com/unique-URI
it needs to redirect to:
https://example2.com/anotheruniqueURI
Currently I have this:
RewriteEngine On
#This redirect works successfully
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^(.*)$ http://example2.com/something [R=301,L]
#This attempt to redirect requests with the specific URI does not work.
RewriteCond %{HTTP_HOST} ^www\.example\.com
RewriteCond %{REQUEST_URI} ^/cars-application$ [NC]
RewriteRule ^/(.*)$ https://example2.com/anotherURI/ [R=301,NC,L]
I've tried many different combinations inside my RewriteRule such as explicitly stating the URI like I did in the RewriteCond above but that doesn't work. Using $1 here won't apply since I'm redirecting to a completely different domain and URI. The URI's I am expecting will be unique. Could you guys provide me some pointers. Is my regex correct or is my rewrite rule capture just wrong?
Your rule failed to work due to the leading slash in your RewriteRule's pattern . Remove the slash to fix it.
RewriteRule ^(.*)$ https://example2.com/anotherURI/ [R=301,NC,L]
Assuming you are redirecting from within a virtualhost of the first domain, you may just do the following:
Redirect permanent /unique-URI http://www.domain2.com/newlocation

RewriteCond when REQUEST_URI do not match htaccess apache2

I have a multilingual wordpress website and want to redirect website of given region to given language,
xyz.de --> xyz.de/de/
xyz.co.uk --> xyz.co.uk/en/
direct access to xyz.de/de and xyz.co.uk/en are working properly. So there is no problem on wordpress side.
Now, I am trying to change the htaccess file of xyz.de and xyz.co.uk so that they redirect the website.
Considering xyz.co.uk
I want to add a RewriteCond such that whenever there is no /en trailing after xyz.co.uk it will automatically add /en.
For example xyz.co.uk/<trailing address> results in xyz.co.uk/en/<trailing address>
So far I have the following code, which somehow doesn't seem to work,
RewriteCond %{REQUEST_URI} !^/en
RewriteRule ^(.*)$ http://xyz.co.uk/en/$1 [L]
The negation of /en is not working! I have also tried
RewriteCond %{REQUEST_URI} !/en
RewriteRule ^(.*)$ http://xyz.co.uk/en/$1 [L]
Could someone tell me where I am going wrong? seems like I have gone wrong in writing RegEx and suggest if there is better way to achieve the same, that does not affect the SEO across different domains.
Use THE_REQUEST variable instead of REQUEST_URI:
RewriteCond %{HTTP_HOST} \.co\.uk$ [NC]
RewriteCond %{THE_REQUEST} !/en/ [NC]
RewriteRule ^ /en%{REQUEST_URI} [L,R=302,NE]
Make sure to keep this rule as your very first rule in .htaccess.
Change it to R=301 once you've tested.

Tricky URL rewrite, regex .htaccess

I have a tricky issue redirecting some URLs internally for my site.
The situation is this, I currently have a URL like example.com/check/youtube.com which internally redirects to check.php?domain=youtube.com using the following mod_rewrite code:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]
RewriteRule ^offline offline.php [NC,L]
RewriteRule ^error error.php [NC,L]
RewriteRule ^check/(.*)$ check.php?domain=$1 [NC,L]
However I would also like to be able to redirect to check.php using a URL like example.com/youtube.com. Unfortunately it is just beyond me to figure it out.
I have a directory /assets/ with all the CSS, JS, etc. which shouldn't be affected.
Thanks
Try this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^/.]+\.[^/]+$ check.php?domain=$0 [L]
This rule rewrites any request with a URL path of the form [^/.]+\.[^/]+ (a string that contains at least one dot but no slashes at all) that cannot be mapped to an existing file to your check.php.
As you want to redirect "example.com/youtube.com" does that mean you wish to redirect pretty much anything? What is specifically allowed to be passed, e.g. would I be allowed to pass "example.com/youtube.com/foobar.php" for a redirect to check.php?