I'm going around in circles with this and could do with someone guiding me through it .
I am trying to redirect any call to .co.uk or uk, as well as http to https:// , with the added complication of codeigniter and removing /index.php.
I have this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1
RewriteCond %{HTTP_HOST} ^(www\.)?example\.co\.uk$ [NC]
RewriteRule ^(.*)$ https://www.example.uk/$1 [R=301,L]
But access to http://example.uk does not redirect a) to https.. and b) seems to break any CSS links
http://www.example.co.uk seems to redirect to http://www.example.uk/www.example.co.uk
Really can't get my head around this.. would appreciate some help!
Assuming you want to always redirect to https://www.planandshop.uk/ I think you don't need to bother with this:
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
since you are explicitly redirecting to https://www.example.uk at the end anyway. Just have to make sure that the final rule will match.
To handle both example.co.uk and example.uk you could write the rule like this:
RewriteCond %{HTTP_HOST} ^(www\.)?example(\.co)?\.uk$ [NC]
RewriteRule ^(.*)$ https://www.example.uk/$1 [R=301,L]
Just like you made the www. optional.
Related
I want to make redirection from http to https.
I try to use this .htaccess code
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
but that show the public folder in the url,
more explain my site is www.example.com white this code is going like this www.example.com/public/
I have this code is my .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
and I want to make the redirection to a specific domain, if the users try for example : example.com, I need to redirect to www.example.com
any solution ?
That is happening because you're also requesting the URI in the RewriteRule. You need to change the rules to the following:
RewriteEngine On
RewriteCond %{HTTPS} !on [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
You can see this rule working here with the /public/ URI and it going to https://example.com only. Make sure you clear your cache before testing this.
I'm using .htaccess to redirect
http://www.example.com/foo/
to
http://www.example.com/foo/bar
This is my code:
redirect 301 /foo/ http://www.example.com/foo/bar
However this produces a feedback loop, something like
http://www.example.com/foo/barbarbarbarbarbar etc.
I've tried placing delimiters around it:
redirect 301 ^/foo/$ http://www.example.com/foo/bar
but then the redirect simply doesn't take place. I'm probably missing some very simple point of syntax. Any ideas? Thanks.
EDIT
Here's my (almost) full .htaccess file:
RewriteEngine On
RewriteBase /
# Canonical is www version
RewriteCond %{HTTP_HOST} !^www.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
#redirect => http unless special page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/(javascripts|images|library|stylesheets)
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#redirect => https for special pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# send to router
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
Have your rules like this:
RewriteEngine On
RewriteBase /
# Canonical is www version
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R=301]
#redirect => http unless special page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} !/(javascripts|images|library|stylesheets)
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
#redirect => https for special pages
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTPS} off
RewriteRule !^index\.php$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^foo/?$ /foo/bar [L,NC,R=301]
# send to router
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
make sure to test this in a new browser or clear your browser cache before testing.
For your information, it really depends on your hosting provider. It may be behind a Load Balancer and you don't have the proper env var set (like HTTPS and others...).
In my case (Infomaniak), nothing actually worked and I got infinite redirect loop.
The right way to do this for Infomaniak is actually explained in their support site:
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule (.*) https://your-domain.com/$1 [R=301,L]
So, always check with your hosting provider. Hopefully they have an article explaining how to do this. Otherwise, just ask the support.
It depends on what is the resource that you want to expose with your redirect.
Is it a file or a directory.
if you would expose a directory under bar you should try :
RedirectMatch 301 ^/foo/ http://www.example.com/foo/bar/
if you would redirect to a file you should use this syntax :
redirect 301 /foo http://www.example.com/foo/bar
you can see this post for more informations
Instead of using redirect, you can use passthrough.
RewriteRule ^/foo$ /foo/bar [PT]
I have a very simple question that for some reason I cannot figure out, and hours of searching has not helped either. Using an .htaccess file, how can I redirect just /login.php and /index.php to https, and then redirect any other page to just http? I currently use this code to redirect to https, but it redirects every page:
RewriteCond %{SERVER_PORT} !443
RewriteRule ^(.*) https://www.ruxim.com/$1 [R]
thank you very much.
The %{SERVER_PORT} variable depends on the UseCanonicalPhysicalPort in your config. If it's not setup, then you may not be able to match against that variable, easier to use %{HTTPS} instead.
RewriteCond %{HTTPS} off
RewriteRule ^/?(login|index)\.php https://www.ruxim.com%{REQUEST_URI} [L,R]
RewriteCond %{HTTPS} on
RewriteRule !^/?(login|index)\.php http://www.ruxim.com%{REQUEST_URI} [L,R]
If you don't need the redirect to non-https, then you don't need the second rule.
Try something like this;
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{REQUEST_FILENAME} =index.php [OR]
RewriteCond %{REQUEST_FILENAME} =login.php
RewriteRule .* https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
I'm note 100% sure if the [OR] will comply suddenly in the middle.
Please apply following conditions to secure only /login.php and /index.php pages. Other pages will be work on HTTP path (non-secure pages).
RewriteEngine On
RewriteBase /
# force https for /login.php and /index.php
RewriteCond %{HTTPS} =off
RewriteRule ^(index|login)\.php$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# don't do anything for images/css/js (leave protocol as is)
RewriteRule \.(gif|jpe?g|png|css|js)$ - [NC,L]
# force http for all other URLs
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} !^/(index|login)\.php$
RewriteRule .* http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Here is my .htaccess file
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ http://www.domain.com$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^. /archive/index.php [L]
going to domain.com will redirect me to www.domain.com
however, going to domain.com/2011/11/18/blog-title will show http://domain.com/var/htdocs/public_html/ instead in the browser URL.
My objective is any page at domain.com will redirect to www.domain.com
and wether I go to www.domain.com or domain.com /YYYY or /YYYY/MM or /YYYY/MM/DD will pass a PHP REQUEST_URI so I can get data from a MySQL database.
I originally copied the .htaccess file from WordPress but it doesn't seem to work properly since /var/htdocs/public_html appears in the browser URL bar.
This will do the trick:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Or this if you want a specific domain:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
The rules are from Drupal 7 and Drupal 6 respectively and have always worked well for me
EDIT
Looking at your code again I think the only problem is that you don't have a / between http://www.domain.com and $1. Other than that it's pretty much identical to the second example above which definitely works
I recently got an alternate domain (parked) and would like it to behave like this:
on a request for "alt-domain.com/" (root): serve "main-domain.com/fakeroot" without a redirect (200)
on a request for "alt-domain.com/anyotherpage": serve "main-domain.com/anyotherpage" with a redirect (301)
I tried this, but it doesn't work:
RewriteCond %{HTTP_HOST} ^(www\.)?alt-domain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.)$ /fakeroot.php [L]
RewriteCond %{HTTP_HOST} ^(www\.)?alt-domain\.com [NC]
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^(.*)$ http://www.main-domain.com/$1 [L,R=301]
Each rule works on its own, but when both are present the request for root also get a redirect. I tried inverting the order, tried skipping [S=1], tried [NS], but no luck. That's when I realized I'm not an htaccess expert, nor a regex expert.
Any help would be much appreciated.
D.
The problem is that the L flag probably doesn't work like you expect, and when mod_rewrite re-examines your rules, RewriteCond %{REQUEST_URI} !^/$ matches because the %{REQUEST_URI} is updated to /fakeroot.php after the initial rewrite.
There are a few different ways to fix this, but I believe this should work well enough, and it doesn't involve changing much:
RewriteCond %{HTTP_HOST} ^(www\.)?alt-domain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/$
Rewriterule ^(.)$ /fakeroot.php [L]
RewriteCond %{HTTP_HOST} ^(www\.)?alt-domain\.com [NC]
RewriteCond %{REQUEST_URI} !^/(fakeroot\.php)?$
RewriteRule ^(.*)$ http://www.main-domain.com/$1 [L,R=301]