RewriteRule changing the URL instead of mapping to a file - apache

I've encounter a weird behavior that I cannot explain nor correct. I need to redirect every HTTP request to HTTPS. I've use the following code :
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# The query string in the rewrite is for testing purposes
RewriteRule (.*) /index.php?url=$1&%{REQUEST_URI}&http=%{HTTPS} [L]
So far, it works. Then, I need a single page to be HTTP, so I added some rewrite conditions :
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/not-https
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/not-https
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /index.php?url=$1&%{REQUEST_URI}&https=%{HTTPS} [L]
Now, here what's happening. For some reasons, when accessing the /not-https page, it redirects to /index.php?url=not-https&/not-https&https=off
Here is a map of GET requests followed by the redirects / displayed URL.
GET: http://example.com/test
-> https://example.com/test with proper $_GET
GET: http://example.com/test.jpg
-> https://example.com/test.jpg with no $_GET (file exists)
GET: https://example.com/not-https
-> http://example.com/not-https
-> http://example.com/index.php?url=not-https&/not-https&https=off
My question is why does the not-https change the displayed URL (and therefor, mess up my application)?

It is happening because value of REQUEST_URI variable is changing to /index.php?... in last rule that makes condition !^/non-https succeed in 2nd rule and makes it execute that rule.
Change your 1st 2 rules to this:
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} \s/+not-https [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !\s/+not-https [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
Unlike REQUEST_URI variable THE_REQUEST doesn't change it's value after execution of other internal rewrites.

Related

RewriteRule - remove params keys and keep values in address bar

RewriteEngine ON
RewriteRule ^video$ video.php [L]
the above line works
example.com/video is interpret as example.com/video.php
now I need example.com/video?id=5&s=lorem-ipsum
to interpret as example.com/video/5/lorem-ipsum - and vice versa
RewriteCond %{THE_REQUEST} video?id=([^\s&]+)&s=([^\s&]+) [NC]
RewriteRule ^ %1/%2? [R=301,L]
RewriteRule ^([\w-]+)/([\w-]+)/?$ video?id=$1&s=$2 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/?$ video?/$1/$2 [L,QSA]
doesn't work - in both directions
pls help
With your shown attempts, please try following .htaccess rules file. These rules are assuming you want to infernally rewrite to index.php you can change php file name in 2nd set of rules as per your requirement.
Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##External redirect Rules from here..
##Redirect 301 to example.com/video/5/lorem-ipsum from example.com/video?id=5&s=lorem-ipsum
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} \s/(video)\?id=([^&]*)&s=(\S+)\s [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]
##Internal rewrite for example.com/video?id=5&s=lorem-ipsum
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^video/([^/]*)/(.*)/?$ index.php?id=$1&s=$2 [QSA,NC,L]

Remove .php extension, force non-slash, force non-www, force https with .htaccess

I have been looking into this for quite a while now, but i just can't get it right. When I have one part working (e.g. forcing non-php), the other part stops working (e.g. forcing non-slash).
What I want exactly:
remove .php extension (so example.com/foo.php becomes example.com/foo in the browser url bar)
remove the slash at the end of a URL (so example.com/foo.php/ becomes example.com/foo. BUT when it's a directory with an index.php inside, it has to redirect accordingly (i can't get this last part to work)
force non-www (this works)
force https (this works)
What I my .htaccess looks like right now:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ /$1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I experience the following problems with this:
example.com/foo.php/ redirects to example.com/foo.php instead of example.com/foo
example.com/foo.php/foo displays the example.com/foo.php file, while example.com/foo/foo displays a 404 message (the latter would be correct in my case)
example.com/admin (this is a folder with an index.php in it) displays a 404 message
I appreciate your help!
Okay, so I know it is a broad question. But if anyone ever searches for this, I almost have it (the only thing that doesn't work is redirecting domain.com/foo.php/foo to domain.com/foo/foo
Here is the .htaccess file I have right now:
RewriteEngine On
Options -Indexes
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /([^/]+)\.php(\?|\ |$)
RewriteRule ^ /%2 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ /$1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

.htaccess Adding HTTPS forcing to existing rewrite rules of subdirectory

Probably I'm overseeing something very basic, htaccess is not my field of expertise.
My site is https-only, to achieve this, the root folder / contains these lines:
RewriteEngine On
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*) https://%{HTTP_HOST}/$1 [R=301,L]
This works just fine. Now we have a new folder called /projects, which contains its own htaccess rules to translate the url in a database query by a file called /projects/project.html
The working htaccess within the folder /projects is:
RewriteEngine On
RewriteBase /projects/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ project.html?project=$1 [QSA,L]
Now I would like to force https connection on this one, because the https rule in the main directory is not applied on /projects if you visit by http://URL directly. Therefore I modified the file to:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://example.com%{REQUEST_URI} [R=301,L,NE]
RewriteBase /projects/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ project.html?project=$1 [QSA,L]
With this, it switches to https, but I keep getting "ERR_TOO_MANY_REDIRECTS".
When I change the lines as #thickguru suggested, like this:
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
It will redirect me to https://example.com/index.html right away.
I fiddled around with the settings, also with the information of this post, however still I can't get it to work.
I just noticed it the variable you are matching against is %{ENV:HTTPS} , so , you can use the following rule in /projects/.htaccess :
RewriteEngine on
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R]
Clear your browser cache or try a diffrent browser to test this.
Use this in your .htaccess:
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://www.example.com%{REQUEST_URI} [R=301,L,NE]
Also, place this above your your other RewriteRule, so that your .htaccess will look like:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://www.example.com%{REQUEST_URI} [R=301,L,NE]
RewriteBase /projecten/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ project.html?project=$1 [QSA,L]
EDIT - Try this:
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]

.htaccess with multiple rewrite rules, based on path

I have the following .htaccess, which is not working as expected:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.bg$
RewriteRule (.*) http://www.example.bg/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/sessiontest\.php$
RewriteCond %{REQUEST_URI} ^/login.* [OR]
RewriteCond %{REQUEST_URI} ^/admin.*$
RewriteRule ^(.*)$ travelpartner.php [L,NC]
RewriteCond %{REQUEST_URI} !^/sessiontest\.php$
RewriteCond %{REQUEST_URI} !^/login.*$
RewriteCond %{REQUEST_URI} !^/admin.*$
RewriteRule ^(.*)$ perla/$1 [L,NC]
What I need is the following:
If the URI is missing the "www" from example.bg, then add it and redirect to www.example.bg
If the URI does not end in "sessiontest.php" and starts with "www.example.bg/login" or starts with www.example.bg/admin, then forward all requets to travelpartner.php
For all other cases, we should forward all requests to a subfolder, called "perla"
However, right now what happens is that we always see the contents of the perla subfolder, even if I open exemple.bg/login
What am I missing here?
With your current rules I get the infinite redirect loop:
Once the rule No.2 is matched, and the request /login is rewritten to /travelpartner.php, the rewritten request is handed back to the URL parsing engine and the ruleset is run again from the start.
This time, it will match the last segment and be rewritten to /perla/travelpartner.php,and sent back again, and be rewritten to /perla/perla/travelpartner.php, etc...
The fix is to, in the last segment, prevent rewriting if the request starts with /perla or is /travelpartner.php:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.bg$
RewriteRule (.*) http://www.example.bg/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/sessiontest\.php$
RewriteCond %{REQUEST_URI} ^/login [OR]
RewriteCond %{REQUEST_URI} ^/admin
RewriteRule ^ travelpartner.php [L,NC]
RewriteCond %{REQUEST_URI} !^/sessiontest\.php$
RewriteCond %{REQUEST_URI} !^/login
RewriteCond %{REQUEST_URI} !^/admin
RewriteCond %{REQUEST_URI} !^/perla/
RewriteCond %{REQUEST_URI} !^/travelpartner\.php$
RewriteRule ^(.*)$ perla/$1 [L,NC]
You can use the following rules
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.bg$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R]
RewriteRule ^(login|admin) /travelpartner.php [L]
RewriteRule !(perla/|travelpartner\.php) /perla [L]

Apache .hatccess rewrites not working for legacy URLS

I'm trying to rewrite some legacy Joomla URLs on a site that's now using ExpressionEngine as its CMS but they're not working.
The ExpressionEngine URL rewrites, i.e. removing index.php from the URL work fine though.
This is what I've got:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
# This is the one that's not working
RewriteRule /index.php?option=com_chronocontact&Itemid=54 /contact/ [R=301,L]
# Force www
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteCond %{HTTP_HOST} (.+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
# Redirect index.php Requests
RewriteCond %{THE_REQUEST} ^[^/]*/index\.php [NC]
RewriteCond %{THE_REQUEST} ^GET
RewriteRule ^index\.php(.+) $1 [R=301,L]
# Standard ExpressionEngine Rewrite
RewriteCond %{REQUEST_URI} ^/
RewriteCond %{QUERY_STRING} ^(gclid=.*)
RewriteRule ^(.+) /index.php?/ [L,PT]
RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(assets|css|images|tinymce|js|min|cms|themes|index\.php|admin\.php|favicon\.ico|index\.php|path\.php|php\.ini) [NC]
RewriteRule ^(.+) /index.php?/ [L]
</IfModule>
Can anyone spot what I'm doing wrong?
The first thing is the stray RewriteCond %{HTTPS} !=on that you have at the top. It looks like it belongs to the rule under it, as in:
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteCond %{HTTP_HOST} (.+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
As far as the rule that you have commented that doesn't work, the ? is a reserved character for regular expressions, and your pattern actually says that the second p in /index.php is "optional". Additionally, you can't match against the query string in a rewrite rule, you need to use a rewrite condition and match against the %{QUERY_STRING} variable:
RewriteCond %{QUERY_STRING} ^option=com_chronocontact&Itemid=54$
RewriteRule ^(index.php)?$ /contact/? [R=301,L]
is probably more along the lines of what you're looking for.