How to change url path and have https:// instad of http? - .htaccess

Im trying to change my url path from:
example.com/path/index.php
or
http://example.com/path/index.php
to
https://example.com/
-
and from
www.example.com/path/index.php
or
http://www.example.com/path/index.php
to
https://www.example.com/path/index.php
This is what I have so far, it works fine for the https:// part but not the changing my url path
.htaccess file
#First rewrite any request to the wrong domain to use the correct one (here www.)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Now, rewrite to HTTPS:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Change to / instead of /path/index.php (Doesn't work.)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ path/index.php?/$1 [L]

I can not comment yet so I ask it here:
Your question / requirement will end up with serving requests in 2 dommain with www and domain w/o www:
example.com/path/index.php
or
http://example.com/path/index.php
to
https://example.com/
which return to the landing page --- no rediction to www.example.com, is this right?
and from
www.example.com/path/index.php
or
http://www.example.com/path/index.php
to
https://www.example.com/path/index.php
simply to force https enabled?
If it's true, then your code below
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
is adding www to the host and not related to the requirement/question you post. Can you make your question clearer?

Related

htaccess redirect to index.php & force https/www problems

In my .htaccess file, I'm trying to achieve 2 things:-
1) Redirect any path to /index.php and pass the path as a query string, but keep the original URL
For example example.com/foo/bar would result in example.com/index.php?foo/bar behind the scenes but still show example.com/foo/bar in the address bar as the URL.
2) Enforce https & www
For example http://example.com/foo/bar would result in https://www.example.com/index.php?foo/bar behind the scenes but would show https://www.example.com/foo/bar in the address bar as the URL.
Below is what I have so far.
# Redirect to index
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
# When http isn't specified, it redirects to file with same URL
RewriteRule ^(.*)$ index.php?$1 [QSA]
## If no http2
RewriteCond %{HTTPS} off [OR]
## Or if http_host isn't www.
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
## Then rewrite
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=302]
With this, the redirect to index.php works as intended provided https and www are present in the original URL. This is because only the first rule block is triggered, whilst the second is skipped.
Though if either https or www aren't present, the resulting URL in the address bar is https://www.example.com/index.php?foo/bar not https://www.example.com/foo/bar as both rule blocks are triggered.
I'm wondering if these 2 different .htaccess rule blocks can be combined into one which fulfils the original requirements?
Thanks
Use this :
RewriteEngine on
# non www to www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# http to https
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
# foo/bar to index.php?a=foo&b=bar
RewriteRule ^([^/]*)/([^/]*)$ /index.php?a=$1&b=$2 [L]
For more rewriting URL logic use this https://www.301-redirect.online/htaccess-rewrite-generator
Recommended tool : https://www.generateit.net/mod-rewrite/

htaccess force https non-www in single redirect for all pages

I would like to redirect everybody to my https:// site (not https://www) and force HTTPS in single redirect. My relevant parts of .htaccess:
# Check if HTTPS and WWW
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [OR,NC]
RewriteCond %{https} off
# Force HTTPS and remove WWW
RewriteRule ^(.*)/$ https://example.com/$1 [R=301,L]
The problem? It doesn't work for sub-pages!
While it does work for main page (eg. http://www.example.com redirects to https://example.com), it doesn't work for sub-pages (eg. http://www.example.com/contact sholud redirect me to https://example.com/contact but only www is removed and I end up on non-https site: http://example.com/contact).
How to change my .htaccess rules, to force HTTPS and remove WWW for my site and all pages of the site in single redirect?
After changes: it works for sub-pages, but in 2 redirects. Can it be done in only one?
Thanks to answer by Nisarg and some digging, my site now properly redirects to HTTPS even on sub-pages. Yay!
Relevant .htaccess code now looks like this:
RewriteEngine On
RewriteCond %{HTTPS} =off [OR]
RewriteCond %{HTTP_HOST} !^example\.com$
RewriteRule ^(.*)$ "https://example.com/$1" [R=301,L]
# remaining htaccess mod_rewrite CODE for WordPress
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Unfortunately, it takes 2 steps to process. So, the question is still up: can it be done in 1 redirect?
Try the following:(Guessing you are using index.php)
RewriteEngine On
# Redirect to www
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
#
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#
# If the requested path and file is not /index.php and the request
# has not already been internally rewritten to the index.php script
RewriteCond %{REQUEST_URI} !^/index\.php
# and the requested path and file doesn't directly match a physical file
RewriteCond %{REQUEST_FILENAME} !-f
# and the requested path and file doesn't directly match a physical folder
RewriteCond %{REQUEST_FILENAME} !-d
# internally rewrite the request to the index.php script
RewriteRule ^ index.php [L]
Make sure you clear your browser cache before testing this change.

.htaccess not redirecting from old url to new url

i have developed a website using codeigniter. Previously the site had a long URL structure so i have made them shorter in the new website.
Although i used the redirect directive in htaccess it gives me a 404 error. I have removed all the old controllers and functions.
below is a few lines from htaccess (there are many urls redirecting to new ones)
RewriteEngine On
# Redirect non-www urls to www
RewriteCond %{HTTP_HOST} ^mysite\.com [NC]
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
#RewriteRule ^([^_]*)_(.*)$ /$1-$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
Redirect 301 /payments/charity_and_donations/paid http://www.mysite.com/charity
Redirect 301 /outwards/office_furniture/damaged http://www.mysite.com/office_assets
Redirect 301 /funding/business_and_person/inward http://www.mysite.com/funds
can someone tell me why it is not redirecting from the old to the new and what am i doing wrong?
You need to have your redirecting happen before you route URIs to /index.php. Also, since Redirect is part of mod_alias and your other redirect/routing rule is mod_rewrite, the URI is being processed twice when it's not supposed to. You should just use mod_rewrite and add the rules to the beginning:
RewriteEngine On
# Redirect non-www urls to www
RewriteCond %{HTTP_HOST} ^mysite\.com [NC]
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]
RewriteRule ^payments/charity_and_donations/paid http://www.mysite.com/charity [R=301,L]
RewriteRule ^outwards/office_furniture/damaged http://www.mysite.com/office_assets [R=301,L]
RewriteRule ^funding/business_and_person/inward http://www.mysite.com/funds [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
Try the below code, this works perfectly for me
RewriteCond %{HTTP_HOST} !^www\.yoursite\.com$ [NC]
RewriteRule ^(.*)$ http://www.yoursite.com/$1 [L,R=301]

multiple errors, not redirecting, apache2/htaccess

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

Apache rewrite all URL's to https with www + a few exceptions

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.

Resources