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

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.

Related

Htaccess rewrite with %REQUEST_FILENAME condition

I stucked. I need to:
all request with domain.com/api/... rewrite to public/ directory
all others request rewrite to frontend/index.html
but if request is a file, then rewrite just to 'frontent/'
I had two htaccess files:
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/api/ [NC]
RewriteRule ^(.*)$ public/$1 [L]
RewriteRule ^(.*)$ frontend/$1 [L]
</IfModule>
frontend/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
# Send Requests To Front...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [L]
</IfModule>
It was working but now I need to remove the one from frontend directory without loosing it's rules. I changed the main .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
# Send request to backend
RewriteCond %{REQUEST_URI} ^/api/ [NC]
RewriteRule ^(.*)$ public/$1 [L]
# Send request to frontend
RewriteCond frontend/%{REQUEST_FILENAME} !-d
RewriteCond frontend/%{REQUEST_FILENAME} !-f
RewriteRule ^ frontend/index.html [L]
# Request for file
RewriteRule ^(.*)$ frontend/$1
</IfModule>
But it doesn't work. I think that this conditions are wrong:
RewriteCond frontend/%{REQUEST_FILENAME} !-d
RewriteCond frontend/%{REQUEST_FILENAME} !-f
because all request (others than api) are rewrited to frontend/index.html which i mentioned.
Could anybody show me where did I mistake?

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.

strange redirect with url rewrite

My .htaccess file is:
RewriteEngine on
RewriteRule (.*) index.php?_url=$1 [QSA,L]
It works perfectly in most cases. But if the requested url exists as a real folder, it goes crazy.
For example when i request /random it is rewritten to index.php?_url=random with status code 200, but for /static (which is an existing directory on the web server) it gives a 301 redirect to /static/?_url=static
use this below code for .htaccess
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
If you want to exclude existing files and directories from being rewritten, do so with a RewriteCond
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?_url=$1 [QSA,L]

.htaccess ensure www redirect incorrect

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. ;)

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]