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
Related
I have two domains, autodromodifranciacorta.it and franciacortacircuit.com both pointing to the same website hosted on this IP address: 94.23.64.40.
Now i want all the content to be under one single domain, so i decided to 301 redirect all the traffic from franciacortacircuit.com to autodromodifranciacorta.it
Here is my .htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# Redirect Entire Site to New Domain
RewriteCond %{HTTP_HOST} ^franciacortacircuit\.com$ [NC]
RewriteCond %{HTTP_HOST} ^www\.autodromodifranciacorta\.it$ [NC]
RewriteRule ^(.*)$ http://autodromodifranciacorta.it/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
The redirect is not working and i have no clue why, because the syntax looks correct to me.
The .htaccess file is processed, because if i put a typo in it, i get server error.
Wha'ts wrong with it?
Your second http_host condition is wrong and it never matches if the current host is franciacortacircuit.com, You need to use an OR condition to match against 2 diffrent hosts.
#Redirect Entire Site to New Domain
RewriteCond %{HTTP_HOST} ^franciacortacircuit\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www\.autodromodifranciacorta\.it$ [NC]
RewriteRule ^(.*)$ http://autodromodifranciacorta.it/$1 [R=301,L]
I am using following htaccess file to achieve some redirections etc:
RewriteEngine On
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTPS} !=on
RewriteRule .* https://www.myurl.com/$1 [R,L]
RewriteRule ^$ /home [R=301,L]
Lately i ve added the latest line so when user visits https://www.myurl.com he gets redirected to https://www.myurl.com/home for the homepage. This works fine as wanted but i have a bad feeling that this should be written somehow better. I don't like the ^$ part but it works. How could i rewrite this?
The HTTPS part can definitely be rewritten and should come first as you're creating multiple redirects.
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
#redirect all traffic looking for a domain not starting with www &
#all non-https traffic to the https://www domain
RewriteCond %{HTTPS} off
#This condition is better for analytics tracking and SEO (single subdns)
RewriteCond %{HTTP_HOST} !^www\. [OR]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteRule ^$ home [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Since your MVC is likely using index.php to process all requests it's likely that the redirect to /home will not work unless you're visiting the website without requesting any file (the root only). If someone explicitly requests https://www.myurl.com/index.php your rule that says RewriteRule ^index\.php$ - [L] tells the site to not change anything. Since you said this is working as expected I've not changed the order.
If it doesn't work as intended, then switch the order of the rule for /home.
RewriteRule ^$ home [R=301,L]
RewriteRule ^index\.php$ - [L]
I am using Cakephp 2.0 and my website domain name is www.sample.com , If I try to access sample.com (with out www) then its going to www.sample.com this is fine. But My problem is my domain consists of lot of pages
for example :
> http://www.sample.com/users/login
> http://www.sample.com/users/add
If I access the above url like http://sample.com/users/login then it redirect to
> http://www.sample.com/index.php?url=users/login
but it needs to redirect to
http://www.sample.com/users/login
I already written the following code .htaccess file(before the app folder) to redirect
Rewritecond %{http_host} ^sample.com [NC]
Rewriterule ^(.*)$ http://www.sample.com/$1 [R=301,NC]
Inside webroot folder I have one .htacces file that contain the following code
may be that is the problem I think
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
Are you trying to always remove www or always keep www? This will add it:
## Add www
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
## CakePHP
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
Try to write the next rule:
RewriteCond %{HTTP_HOST} ^sample.com [NC]
RewriteRule ^(.*)$ http://www.sample.com/$1 [L,R=301]
on app/webroot/.htaccess file, not on app's folder .htaccess. It works perfectly for me.
I'm having a strange problem with an ExpressionEngine 1.6 site. I have added a page, and some people have had difficulty accessing the page with www. in front of the URL. Without the www, there seem to be no problems. It also seems to depend on which browser I am using - if I use Chrome, I cannot see the page with www in the URL, but I can if there is no www. Other browsers work whatever. I've tried clearing cache in Chrome to no avail.
What on earth could the problem be?
Here's the code in .htaccess, in case it's something to do with that.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteRule ^(.*)$ /index.php/$1 [QSA,L]
RewriteCond %{QUERY_STRING} ^utm_medium
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteRule (.*) /index.php?/pages/index/&%{QUERY_STRING} [L]
</IfModule>
It's not ideal to have a site that's accessible on both the www and no www as this will mean your Google listing gets diluted due to duplicate content. By eliminating the www you will also resolve your issue:
# Remove the www from the URL
# ------------------------------
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Should go after the line "RewriteBase /" and before your main rewriting.
The alternative is to add the www:
# Add the www to the URL (use instead of www removal)
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^localhost [NC]
RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
That said, to try and specifically solve the issue, I'm assuming that other pages are fine and it's just this page in particular? Also can the page be accessed by including the index.php/ in the path?
This rewrite seems a bit strange:
RewriteCond %{QUERY_STRING} ^utm_medium
RewriteCond %{REQUEST_URI} ^/$ [NC]
RewriteRule (.*) /index.php?/pages/index/&%{QUERY_STRING} [L]
If this line is the culprit and relevant to the page causing issue, try changing the last line:
RewriteRule ^(.*)$ /index.php?/pages/index/&%{QUERY_STRING} [L]
or
RewriteRule (.*?)index.php?/pages/index/&%{QUERY_STRING} [L]
I've tried all the answers to similar stack questions and nothing has worked. I need to redirect all to https://www except for example.com/blogs/* and example.com/page-name.
I currently have this:
RewriteCond %{HTTPS} =off
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
RewriteCond %{http_host} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
which redirects everything except for https://example.com, it will NOT add the www.
You can see for yourself at https://moblized.com
RewriteEngine On
RewriteCond %{HTTPS} =off
RewriteRule ^(.*)$ https://www.moblized.com/$1 [R=301,L]
RewriteCond %{http_host} ^moblized.com [NC]
RewriteRule ^(.*)$ https://www.moblized.com/$1 [R=301,L]
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} blogs
RewriteRule ^(.*)$ http://moblized.com/blogs/$1 [R,L]
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>
# $Id: .htaccess,v 1.90.2.4 2009/12/07 12:00:40 goba Exp $
AddHandler php5-script .php
Thank you!
I hope I understood you correctly. You want:
redirect from example.com to www.example.com (except /blogs/ and /page-name)
redirect all pages to HTTPS (except /blogs/ and /page-name)
based on your current .htaccess under /page-name you mean /favicon.ico
Here are the rules for the above requirements -- put them into your .htaccess:
# activate rewrite engine
RewriteEngine On
# don't touch favicon.ico (always accept as is regardless of the domain or protocol)
RewriteRule ^favicon.ico$ - [L]
# don't touch /index.php (usually means already overwritten URL)
# otherwise we may enter into a loop
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^index\.php$ - [L]
# ensure trailing slash is present for /blogs -> /blogs/
RewriteRule ^blogs$ http://mobilized.com/blogs/ [R=301,QSA,L]
# /blogs/ should only be accessible via http://example.com/blogs/
RewriteCond %{HTTP_HOST} !^moblized\.com$ [NC]
RewriteRule ^blogs/(.*)$ http://mobilized.com/blogs/$1 [R=301,QSA,L]
RewriteCond %{HTTP_HOST} ^moblized\.com$ [NC]
RewriteCond %{HTTPS} =on
RewriteRule ^blogs/(.*)$ http://mobilized.com/blogs/$1 [R=301,QSA,L]
RewriteRule ^blogs/.* - [L]
# redirect to www.example.com if necessary
RewriteCond %{HTTP_HOST} ^moblized\.com$ [NC]
RewriteCond %{REQUEST_URI} !=/client-ipad-contest
RewriteRule ^(.*)$ https://www.moblized.com/$1 [R=301,QSA,L]
# redirect to HTTPS if not there already
RewriteCond %{HTTPS} !=on
RewriteCond %{REQUEST_URI} !=/client-ipad-contest
RewriteRule ^(.*)$ https://www.moblized.com/$1 [R=301,QSA,L]
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?q=$1 [L,QSA]
BTW, browser most likely will show "Untrusted Certificate" warning if your customer go to https://example.com. This is because HTTPS session has to be fully established first before the request starts processing by Apache's rewrite module.
If that is problem -- then consider buying another SSL certificate (or from another vendor) which will cover both example.com and www.example.com (GoDaddy does this for sure) or get wildcard certificate which will cover all subdomains -- *.example.com (but this most likely will be much more expensive).
UPDATE: After simulating your requirements locally (sorry, I have no SSL with working Apache, so I have replaced it (in my testing) with different kind of rule/domain name) I have revised and updated the rules.
I've tested these rules locally (all pages are very simple, just include 1 image & css and a bit of text) -- everything looking good. Let me know if something does not work.