Setting .htaccess to go to https and php instead of html - .htaccess

I have my hosting with 1and1, I have just got a SSL certificate. I currently have changed the .htaccess to load .php files instead of html files which works fine with this code:
RewriteEngine on
#remove the need for .php extention
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
Now I want the site to load with https, regardless of if it is entered like http://www or http:// etc. I am getting so many different ways to do this from various places as most same that I am supplying duplicate sites if I don't specify either http:// or http://www. So I think I will be solving 2 problems in one go. The code 1and1 supply is:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.sitename\.co.uk$ [NC]
RewriteRule ^(.*)$ https://www.sitename.co.uk/$1 [R=301,L]
But this does not seem to do much on its own? Can I conbine the 2 to simply load https://www.site for everything and change all links to load .php.

This works perfect
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteEngine on
#remove the need for .php extention
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

Related

Why doesn't url rewriting work for .html extensions?

I am trying to get
/pages/Settings.html
to appear as
/Settings
by using the RewriteRule
RewriteRule ^([A-Za-z_]+)$ pages/$1.html [NC] # Handle pages
in the .htaccess file. This does not work. However,
/pages/ANYOTHERFILE.php
works by using
RewriteRule ^([A-Za-z_]+)$ pages/$1.php [NC] # Handle pages
to be rewritten into
/ANYOTHERPAGE
Why does the former not work and the latter work? How do I get .html extensions to be rewritten like this?
Add this rules to top of your .htaccess file
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /pages/$1.html [L]

.htaccess file hide GET request in URL

I am very new to .htaccess files and may have done mine wrong. I have been gathering snippets of code trying to get done what I want.
This is what I have in my file so far
I read this should be in your .htaccess file
Options +MultiViews
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
This was added to force a redirect from HTTP to HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This was added to remove .php at the end of all the files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This was added to remove the showing of /index when the home link was pressed
RewriteCond %{THE_REQUEST} ^.*\/index?\ HTTP/
RewriteRule ^(.*)index\.php?$ "/$1" [R=301,L]
I have tried a lot of ideas I found on both here and other sites from Google. I have had no luck hiding the GET request.
Currently, the URL looks like https://mysite.ca/event?id=123
What I would like it to look like is either https://mysite.ca/event/123
This is hosted on Godaddy and it is just a plain PHP site if that makes any difference.
Any ideas would be great!
First of all Options +MultiViews already removes .php extension so you don't need:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
About your problem try this Rule:
RewriteRule ^event/([0-9]+)?$ event?id=$1 [NC]
Which changes url from example.com/event/<number> to example.com/event?id=<number>
Although, if your site makes infinite redirects then you should change it to:
RewriteRule ^events/([0-9]+)?$ event?id=$1 [NC]
This happens if you have some includes/headers that redirect back to event and creates infinite loop.

htaccess mod url re-writing

I am rebuilding an old website and have a bunch of old url's that I am having issues re-writing properly.
For example, some of my old URLs are structured as the following:
mydomain.com/?x=about-us **and** mydomain.com/?x=services
I would like the examples to rewrite or redirect to the following:
mydomain.com/about-us **and** mydomain.com/services
Essentially, if there is an occurrence of '?x=' in the URL I would like to strip it out.
Currently the only other cond/rule I have in .htaccess is to remove the '.php'. extensions.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
This works well enough, and allows me to write my internal links without the '.php' extension, such as the following:
link
This sends me to 'mydomain.com/about-us', which is what I want. My issue is handling URLs with the '?x='
Any help would be appreciated thank you.
You may use these rules in your site root .htaccess:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+(?:index\.php)?\?x=([^\s&]+)\s [NC]
RewriteRule ^ /%1? [R=301,L,NE]
# add .php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ ?x=$1 [L,QSA]

Issues with URL Writing

I'm currently hosting a site on a service that has mod_rewrite on by default. I edited the .htaccess with
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
but it only allow the site to be accessed with "site.com/pagename." I want it to rewrite the url to say "site.com/pagename" rather than "site.com/pagename.html."
I've tried a million different flavors of this code and it won't rewrite the url to exclude .html. The HTML code has the pages linking to each other as
Text
If that makes a difference.
Thanks in advance for any help.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+\.)+html?\ HTTP
RewriteRule (.+)\.html?$ $1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

Mod re-write on sub folder

I'm trying to mod re-write to a specific sub folder called /checkout this has an SSL certificate attached for https
So I have this in the root folder of my site -
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
# in http: force secure.html to https
RewriteCond %{server_port} !=443
RewriteCond $1 ^([^/]+)$ [NC]
# this kinda works below but is resolving to index2.php and is showing in the url so not really working yet
RewriteRule checkout/([^/]+)\.html$ https://mysite.com/checkout/index2.php [L]
Except that instead of showing something like
https://mysite.com/checkout/index.html
it shows
https://mysite.com/checkout/index2.php
(index2.php is a test url btw)
I've also tried amending the .htaccess in the sub folder to to something like
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{server_port} !=443
RewriteCond $1 ^([^/]+)$ [NC]
RewriteRule ^([^/\.]+)/?$ https://mysite.com/checkout/index2.php [L]
I just want to process
https://mysite.com/checkout/index.html or https://mysite.com/checkout/about-us.html as the same I would in the root like this
https://mysite.com/index.html => https://mysite.com/index.php (and then get the variables etc)
ie
RewriteRule ^/?(catalogue-page)/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+)/([0-9]+) index2.php?friendly_url=$1&catalogue_id=$2&view=catalogue&categoryid=$3&subcategoryid=$4&subjectid=$5&item=$6
I think that https is more complex.
Any pointers/help etc much appreciated
Your RewriteRule seems to indicates that you are trying to take a non-secure url and display a different page via https, this in itself is not possible. Having said that your description doesn't sound like that is the overall aim.
If you wish to force non-secure URLs to be secure, this is fine and it's also fine to want requests for index.html to server index.php. It's not however possible to do this all in one fell swoop.
# first force urls to be secure.
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^checkout/([^/\.]+)/?$ https://mysite.com/%{REQUEST_URI} [R=301,L]
# now rewrite them, so that the requests all get served through the front controller
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^checkout/([^/\.]+)/?$ /checkout/index2.php [L]

Resources