Using htaccess to redirect all pages but one to another domain - apache

My htaccess is as follows:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.es$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.es$
RewriteRule ^(.*)$ http://www.domain.com/es [L,R=301]
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?section=$1 [QSA,L]
Basically it's function is to redirect all domain alias (i.e. domain.mobi) to domain.com, except domain.es, that will be redirected to domain.com/es. Then there's another rewriterule that appends the query string (so in the background, domain.com/test becomes domain.com/index.php?section=test)
My problem is that now I need to exclude a url (www.domain.es/landing) from the redirection (so it stays in domain.es), and I can't make it work. I've tried adding this condition to the first two rules to exclude the page:
RewriteCond %{REQUEST_URI} !^/landing$
but then it goes to www.domain.com/es?section=landing (not only still redirects to domain.com; the query string appears in the browser bar). Any ideas?

You can use these rules:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.es$ [NC]
RewriteCond %{THE_REQUEST} !/landing[\s?] [NC]
RewriteRule ^(.*)$ http://www.domain.com/es/$1 [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{THE_REQUEST} !/landing[\s?] [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?section=$1 [QSA,L]

Related

How to resolve conflicting mod_rewrite rules and conditions

I have a couple of rewrite rules in my .htaccess that seem to be conflicting.
I am running an ExpressionEngine (CodeIgniter) website that resolves all URI's using an index.php file. I have removed the index.php from the URI's for aesthetic reasons.
What I am trying to achieve:
Redirect 301 all pages with a trailing slash (example.com/bla/ =>
example.com/bla)
Remove index.php from all URI's
What I have now:
<IfModule mod_rewrite.c>
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{SERVER_ADDR} !=127.0.0.1
RewriteCond %{SERVER_ADDR} !=::1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond $1 !^(images|system|themes|index\.php|admin\.php|favicon\.ico|robots\.txt|humans\.txt|crossdomain\.xml) [NC]
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
</IfModule>
What works:
All deeplinks are redirected to their non-trailing-slash version
(example.com/bla/ => example.com/bla).
index.php is removed from all deeplink pages.
What does not work:
The homepage (example.com) gives me an error in Google Chrome, saying "Too many redirects".
How do I update the conditions and rules so that I achieve clean links without a trailing slash, and without index.php, regardless of which page it is.
Have it like this:
RewriteCond %{HTTPS} !=on
RewriteCond %{SERVER_ADDR} !=127.0.0.1
RewriteCond %{SERVER_ADDR} !=::1
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteRule ^(.*?)index\.php(/.*)?$ /$1$2 [R=301,NE,L]
RewriteCond $1 !^(images|system|themes|index\.php|admin\.php|favicon\.ico|robots\.txt|humans\.txt|crossdomain\.xml) [NC]
RewriteRule ^(.*)$ index.php/$1 [L]
Clear your browser cache before testing.

How to remove index.php from htaccess when redirect 301 - Codeigniter

I have a problem in Codeigniter when the url type is:
example.gov.ar/news (without www and .gov)
I need it to redirect to:
www.example.gob.ar/news (with www and .gob)
But it redirect
www.example.gob.ar/index.php/news
This is my htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
#non-www to www
RewriteCond %{HTTP_HOST} !^www\.(.*)
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
#.gov.ar to .gob.ar
RewriteCond %{http_host} ^example.gov.ar$ [OR]
RewriteCond %{http_host} ^www.example.gov.ar$
RewriteRule ^(.*)$ http://www.example.gob.ar/$1 [R=301,L]
Note: my config file have $config['index_page'] = '';
Thanks in advance.
Try switching the order of your rules.
RewriteEngine on
#non-www to www
RewriteCond %{HTTP_HOST} !^www\.(.*)
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
#.gov.ar to .gob.ar
RewriteCond %{http_host} ^example.gov.ar$ [OR]
RewriteCond %{http_host} ^www.example.gov.ar$
RewriteRule ^(.*)$ http://www.example.gob.ar/$1 [R=301,L]
# Redirect to index.php internally
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
An external redirect triggered by .htaccess is re-evaluated against all rules, therefore order is important.
In the changed order, the external redirects happen first, so just the final URL is matched against the third RewriteRule, which then does not become visible externally.

Redirect to new url except for on page with existing expression engine redirect

I have the following redirects in the .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} /content [NC]
RewriteRule (.*) http://subdomain.domain1.co.uk/index.php/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/content$ [NC]
RewriteRule (.*) http://www.domain2.co.uk/$1 [R=301,L]
The first redierect needs to be there for the expression engine to work properly. Visit to
subdomain.domain1.co.uk/content matches the rule but I end up in the indefinite redirect loop. Can anybody help ?
Thanks,
EDIT: 2nd and 3rd rule may actually be incorrect. What I want it to do is to redirect anything from http://subdomain.domain1.co.uk/$1 to http://www.domain2.co.uk/$1 expect for http://subdomain.domain1.co.uk/content and http://subdomain.domain1.co.uk/content/*
Keep your rules like this:
RewriteEngine on
# redirect everything except content/? to www.domain2.co.uk
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.domain1\.co\.uk$ [NC]
RewriteCond %{THE_REQUEST} !\s/content/?[\s?] [NC]
RewriteRule ^ http://www.domain2.co.uk%{REQUEST_URI} [R=301,L,NE,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L,QSA]

getting htaccess rewrite rules to rewrite only specific urls

I only want to rewrite this specific url:
http://www.website.co.uk/php/project.php?project=anythingHere
to
http://www.website.co.uk/php/project/anythingHere
this works fine but when I go to http://www.website.co.uk/php/project/anythingHere
it has changed all the links as well (style sheets, javascript, hyperlinks... things I don't want to change). for example:
http://www.website.co.uk/php/index.php
to
http://www.website.co.uk/php/project/index.php
This is what I have already
RewriteEngine On
RewriteCond %{HTTP_HOST} ^website.com\.co\.uk$ [OR]
RewriteCond %{HTTP_HOST} ^www\.website.com\.co\.uk$
RewriteRule ^clients$ "http\:\/\/website.com\.co\.uk\/billing\/index\.php" [R=301]
RewriteRule ^project/([^/]*)$ /php/project.php?project=$1
Can you try this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?website\.co\.uk$ [NC]
RewriteRule ^clients/?$ http://website.co.uk/billing/index.php [NC,L,R=301]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(php/project)\.php\?project=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(php/project)/([^/]+)/?$ /$1.php?project=$2 [L,QSA,NC]

mod_rewrite: HTTPS for only one page

I want to redirect to HTTPS only /login page. Rest of pages needs to be HTTP.
.htaccess file looks like:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} /login
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !/login
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php
I don't know why it doesn't work. When I go to /login I was redirected to example.com/index.php
What is wrong?
Is there an actual (physical) /login folder or file in your directory? I suppose not, so your /login request is finally captured by the last rewrite rule and redirected to index.php.
Try adding this RewriteCond %{REQUEST_URI} !/login above this line:RewriteCond %{REQUEST_FILENAME} !-f ( or just handle the login request differently )
Try adding L flag in your last rule have your code like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^ http://example.com%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} /login [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !/login [NC]
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]