.htaccess ensure www redirect incorrect - apache

I am trying to ensure that urls always have www in front of them for canonicalization reasons.
Unfortunately when I put the following url in:
http://website.net/handbags/1/12/this-is-some-text
It redirects me here:
http://www.website.net/?controller=handbags&path=1/12/this-is-some-text
I would like to add it works fine when using:
http://www.website.net/handbags/1/12/this-is-some-text
I want it to redirect me here...
http://www.website.net/handbags/1/12/this-is-some-text
I'm not sure what could be causing this error. Here is my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# CORE REDIRECT
RewriteRule ^([a-zA-Z]*)/?(.*)?$ index.php?controller=$1&path=$2 [NC,L]
# ENSURE WWW
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+\.[a-z]{2,6})$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
</IfModule>

<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# ENSURE WWW
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+\.[a-z]{7})$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
# CORE REDIRECT
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]*)/?(.*)?$ index.php?controller=$1&path=$2 [NC,L]
</IfModule>
You can do what I've done above; however, it's just going to redirect to http://www.website.net/handbags/1/12/this-is-some-text and then it's immediately going to hit the "CORE REDIRECT" rule and send you to http://www.website.net/?controller=handbags&path=1/12/this-is-some-text.
Is there a reason you don't want it to redirect to "?controller=handbags" when they go to www?
UPDATE
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# ENSURE WWW
Rewritecond %{HTTP_HOST} !^www\.website\.net
RewriteRule ^(.*)$ http://www.website.net/$1 [R=301,L]
# CORE REDIRECT
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]*)/?(.*)?$ index.php?controller=$1&path=$2 [NC,L]
</IfModule>

Whenever your # CORE REDIRECT matches, the other rules are not even checked, as the core redirect rule has the Lflag set. That ones tells Apache to stop trying other rules. Remove that flag or change the rule. ;)

Related

Getting .htaccess redirects from one domain to another to work for WWW ( both http://www. and https://www.), HTTP and HTTPS

I have a scenario for redirection I'm having trouble wrapping my head around.
The goals are:
old-domain.com/store needs to redirect to new-domain.com/store
There are three validation scripts that should remain on Old-Domain.com
Everything else from old-domain.com to redirect to new-domain.com
These rules need to apply to all scenarios HTTP, HTTPS and WWW (HTTP and HTTPS)
The problem I'm having is that the old-domain.com/store only properly redirects to new-domain.com/store when I remove the WWW redirects. I can't seem to get it to all play together nicely.
Here's what I've got:
Options +FollowSymLinks
## Rewrite rules
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.old-domain.com$
RewriteRule ^(.*)$ http://old-domain.com/$1 [R=301]
RewriteCond %{HTTP_HOST} ^www.old-domain.com$
RewriteRule ^(.*)$ https://old-domain.com/$1 [R=301]
RewriteCond %{REQUEST_URI} !^/validation-script-1\.html
RewriteCond %{REQUEST_URI} !^/validation-script-2\.html
RewriteCond %{REQUEST_URI} !^/validation-script-3\.html
Redirect 301 /store/ https://new-domain.com/store
RewriteRule ^(.*)$ https://new-domain.com/ [R=301,L]
</IfModule>
We've also tried the following but then the validation scripts stop passing through:
Options +FollowSymLinks
## Rewrite rules
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/validation-script-1\.html
RewriteCond %{REQUEST_URI} !^/validation-script-2\.html
RewriteCond %{REQUEST_URI} !^/validation-script-3\.html
# Remove trailing slash from non-filepath urls
RewriteCond %{REQUEST_URI} /(.+)/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ http://old-domain.com/%1 [R=301,L]
# Include trailing slash on directory
RewriteCond %{REQUEST_URI} !(.+)/$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)$ http://old-domain.com/$1/ [R=301,L]
RewriteRule ^/?store https://new-domain.com/store [R=301,L]
# Remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [OR,NC]
RewriteRule ^(.*)$ https://new-domain.com/ [R=301,L]
</IfModule>
The following rewrite rule will redirect all (www.)old-domain.com URLs to https://new-domain.com/ except URLs containing validation-script-(1|2|3).html
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/validation-script-(1|2|3)\.html$
RewriteCond %{HTTP_HOST} ^(www\.)?old-domain\.com$
RewriteRule ^(.*)$ "https\:\/\/new-domain\.com\/$1" [R=301,L]
Check this demo https://htaccess.madewithlove.be?share=f0deda97-dff8-569e-bd6f-411b8a779361
Edit:
to redirect old-domain.com/store or old-domain.com/store/ to new-domain.com/pages/store
RewriteRule ^store/?$ "https\:\/\/new-domain\.com\/pages\/store" [R=301,L]

htaccess redirecting all urls except one to a different domain not working

I'm trying to redirect all urls except http://shop.mysite.com/enquiry/ to a different domain like below but it's redirecting everything to "example.com", including the "enquiry" page.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/enquiry/
RewriteRule .* https://www.example.com [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule ^(.*)$ index.php
</IfModule>
So how do I make sure "enquiry" isn't redirected?
Your final RewriteRule for index.php is probably causing an additional redirect to occur in the PHP script.
You could try moving the redirect below this rule:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule ^(.*)$ index.php
RewriteCond %{REQUEST_URI} !^/enquiry/?
RewriteRule .* https://www.example.com [R=301,L]
</IfModule>
In addition, you will need to add some more rewrite conditions to exclude referenced scripts/stylesheets from being redirected too.
For a negative condition, you should THE_REQUEST variable instead of REQUEST_URI:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} !/enquiry[?\s/] [NC]
RewriteRule ^ https://www.example.com [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}/index.html !-f
RewriteCond %{REQUEST_FILENAME}/index.php !-f
RewriteRule ^ index.php [L]
</IfModule>
THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules. Example value of this variable is GET /index.php?id=123 HTTP/1.1
Make sure to clear your browser cache or use a new browser when testing.

htacess Proxy Redirect /a to /b, 301 redirect of /b to /a

I have website built on a bespoke centralised legacy CMS.
It has a section which works under the URL /database
For one site I'd like to change that to use /blog and it still needs to tell the CMS that it's database.
So for .htaccess I have
RewriteRule ^blog$ /database [P,L]
RewriteRule ^blog/(.*)$ /database/$1 [P,L]
Which works great. But I also need to tell google that the site has changed and not to look at the old one.
So I need /database to 301 to /blog.
So:
RewriteRule ^database$ /blog [L,R=301]
I've tested this with http://htaccess.madewithlove.be and it says it's fine, but it's causing an infinite loop in the browser.
To be honest I feel like part of the problem is that I can't really express the question very well, especially the title, so any feedback on that would be appreciated.
Here's the rest of the file just in case that's relevant
# redirect trailing slashes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^(.+)?fromMobile=true$ [NC]
RewriteRule (.*) /$1?%1&%2 [R=301,L]
RewriteRule ^(.+[^/])/$ http://%{HTTP_HOST}/$1 [R=301,L]
#uk.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^iflg\.uk\.com$ [NC]
RewriteRule ^(.*)$ http://www.iflg.uk.com/$1 [R=301,L]
RewriteRule ^blog$ /database [P,L]
RewriteRule ^blog/(.*)$ /database/$1 [P,L]
#RewriteCond %{REMOTE_ADDR} !=localhost
#RewriteRule ^database$ /blog [L,R=301]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /~iflg/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /~iflg/index.php
</IfModule>
You need to exempt the second rule to prevent the proxy server from being redirected too.
RewriteCond %{REMOTE_ADDR} !=127.0.0.1
RewriteRule ^database$ /blog [L,R=301]
Do not rely on that htaccess tester, it is too simplistic.

Treat particular folder as actual folder not as controller CodeIgniter

I want to make a particular folder to be treated as an actual one, not as a controller in codeigniter and also want to redirect my all domain.com requests to www.domain.com. I have the following htaccess code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond $1 !^(FOLDER_TO_BE_EXCLUDED)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Redirect non-www to www:
#RewriteCond %{HTTP_HOST} !^www\. [NC]
#RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
If I uncomment the lines (Redirect non-www to www), all requests are treated as controller
so the folder (FOLDER_TO_BE_EXCLUDED) is also treated as controller. I want to access this folder as an actual one.
If I will not redirect non-www to www I cant access session variables on inner pages.
Hope you will understand.
RewriteCond only apply to the RewriteRule which follows: your rules (commented now) were incorrect.
Besides, IMO, the simplest way to define exceptions like this one, is to use a non-rewriting rule like: RewriteRule ^FOLDER_TO_BE_EXCLUDED/ - [L] (on "top" of rewrite rules)
RewriteEngine On
# Redirect non-www to www:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* http://www.%{HTTP_HOST}/$0 [R=permanent,L]
RewriteRule ^FOLDER_TO_BE_EXCLUDED/ - [L]
# Rewrite all other URLs to index.php/URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php?url=$0 [PT,L]
Try adding a RewriteCond to your last RewriteRule:
RewriteCond $1 !^(folder_name/)
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
This should stop it redirecting www.yourdomain.com/folder_name to the index.php.
You can add more folders to the condition using the | character:
RewriteCond $1 !^(folder_name/|another_folder/)

URL Rewriting to have all requests to http://example.com/controller redirected to http://example.com/index.php/controller without changing the url

I'd like to have all requests to http://example.com/controller redirected to http://example.com/index.php/controller without changing the url, and this is what my .htaccess file looks like:
# Customized error messages.
ErrorDocument 404 /index.php
# Set the default handler.
DirectoryIndex index.php
# Various rewrite rules.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301]
</IfModule>
Unfortunately, this doesn't work. All requests to http://example.com/controller get routed to the home controller, and the url doesn't change.
And all requests to http://www.example.com/controller get routed to http://example.com/index.php/controller and the url DOES change in the address bar.
Put those rules that cause an external redirect in front of those that cause an internal redirect. So:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} =www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]