Merge addition of URL rewriting of sub-domain and single page to capture all URLs - apache

I have presently setup .htaccess according to the answer given here to capture all URLs in a single file. Now I want to capture all URLs having no "www" subdomain and redirect to www.my-site.com.
So, presently I have (to capture all URLs in a single file):
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
And I want to merge the following with it (capture all URLs having no "www" subdomain and redirect to www.my-site.com):
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]

First the redirection, and after the rewrite:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

Related

Adding directory to url with htaccess & rewrite

I have a site based on Modx CMS and I need to create a 301 redirect for the main folder & old links after the multilanguage functionality has been enabled.
What I need to achieve is the default fallback to english eg. make sure that:
example.com/products -> example.com/en/products
example.com/about - example.com/en/about
etc.
I also need to make sure that if there already is a language selection (for example de) in the url, I don't add en to url. (so no example.com/en/de/products)
I am having trouble adding the /en/ to url and I am ending up with infite /en/en/en loops on the URL
To add the /en/ to the url I tried the following.
RewriteCond %{REQUEST_URI} !^/de$
RewriteCond %{HTTP_HOST} .*example.com [NC]
RewriteRule ^https://example.com/en/%{REQUEST_URI} [L,R=301]
It results to a endless /en/en/en loop in the url.
The whole htaccess is as follow:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteBase /
RewriteCond %{REQUEST_URI} !^/de$
RewriteCond %{HTTP_HOST} .*example.com [NC]
RewriteRule ^https://example.com/en/%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Try the code below. The rules are chained (the default is AND). So, if your URL doesn't start with /en, /de, it's not the index (/), and it's not a file name or a directory, do a 301 redirect to the English version:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/en
RewriteCond %{REQUEST_URI} !^/de
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ https://%{HTTP_HOST}/en%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

Using htaccess to redirect all pages but one to another domain

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]

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]

Redirect www to non-www is loosing my other RewriteRule

I have this code in my, .htaccess file.
My url is fine foodzite.dk/vis/1234567
When i enter www.foodzite.dk/vis/1234567 i want it to redirect to foodzite.dk/vis/1234567
but i redirects to foodzite.dk/shop.php?url=1234567
How is it i cant combine the to Rewrites?
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^vis/([a-z0-9\-_]+)/?$ show.php?url=$1 [L,QSA]
RewriteRule ^postnr/([^/]*)$ postal.php?postal=$1 [NC,L]
RewriteRule ^by/([^/]*)$ city.php?city=$1 [NC,L]
RewriteCond %{HTTP_HOST} !^foodzite\.dk$ [NC]
RewriteRule (.*) http://foodzite.dk/$1 [R=301,L]
You need to have your redirect rule before your routing rule. Otherwise the URI gets routed to your php file, then the redirect rule gets applied and the URI is exposed:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^foodzite\.dk$ [NC]
RewriteRule (.*) http://foodzite.dk/$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^vis/([a-z0-9\-_]+)/?$ show.php?url=$1 [L,QSA]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^postnr/([^/]*)$ postal.php?postal=$1 [NC,L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^by/([^/]*)$ city.php?city=$1 [NC,L]
Additionally, your conditions only get applied to the immediately following rewrite rule, so the 2nd and 3rd don't have the same conditions. (Unless you wanted it to be that way).
Try something like:
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^foodzite.dk [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]