mod_rewrite | remove .php and force https - php

I created a little static website in html/js/php.
Now I only want the https:// non www. domain to be used (like this: https://zufallsgenerator.org/). All other combinations should be redirected. At the moment the site can be accessed from every possible combination...
To make the URLs more readable I also want to remove the .php extension.
I'm only able to get one of these things working. As soon as I try to combine them I get too many redirects. At the moment only the removal of the .php extension is online.
I'm using that well known snippet:
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R,L]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
To redirect http to https and non www I normally use:
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Any ideas how to combine them?
Is there a best practice?
I already searched a lot and only found huge .htaccess files with many many rewrite rules...
Thank you so much!

Have it this way:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R=301,L,NE]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Related

.htaccess on host gator is not removing .php extension and adding trailing slash

So I have recently uploaded my new website to hostgator.
I am trying to remove .php extension and add a trailing slash to all my urls.
For example
website.com/about.php should change to website.com/about/
I have the htaccess code that has worked for other websites on different hosts, but when i tried it for this website on hostgator it doesnt work.
This is the code I tried:
RewriteEngine On
RewriteBase /
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R,L]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://website.co.uk/$1 [R,L]
RewriteCond %{HTTP_HOST} ^www.website.co.uk [NC]
RewriteRule ^(.*)$ http://website.co.uk/$1 [L,R=301]
Currently I now have this in my htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [NC,L]
# remove www and turn on https in same rule
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
Hostgator added this htaccess for me which basically redirects http requests to https and removes http://www.website.co.uk and www.website.co.uk to website.co.uk
What can I do to remove .php extension and force trailing slash?
You can replace all the code in your site root .htaccess with this:
RewriteEngine On
# remove www and turn on https in same rule
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^(.*?)/?$ https://%1/$1/ [R=301,L,NE]
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=301,NE,L]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301,NE]
# To internally rewrite /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]
Make sure to test this in a new browser or after clearing your browser cache.
For starters, depending on your hosting environment you may need to add the following to the beginning of your htaccess file:
Options -MultiViews
If MultiViews is on the server will do implicit filename matching. So if you request page/ it could interpret that as page.php. Use the option above to disable MultiViews and see if that helps.

.htaccess rewrite or subdomain

This is my url is shown at the moment;
http://blog.example.co.uk/post?id=152&title=titleofpost
This is my htaccess as shown below;
To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]
Blog subdomain
RewriteCond %{HTTP_HOST} blog.example.com$
RewriteCond %{REQUEST_URI} !^/blog/
RewriteRule ^(.*)$ /blog/$1 [L]
How can I show the url as;
http://blog.example.co.uk/post/titleofpost
Have it like this:
Options -MultiViews
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+post(?:\.php)?\?id=([^\s&]+)&title=([^\s&]+)\s [NC]
RewriteRule ^ /post/%1/%2? [R=302,L,NE]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+author(?:\.php)?\?name=([^\s&]+)\s [NC]
RewriteRule ^ /author/%1? [R=302,L,NE]
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [L,R=302]
# internal forward from pretty URL to actual one
RewriteRule ^post/([\w-]+)/([\w-]+)/?$ post.php?id=$1&title=$2 [L,QSA,NC]
# internal forward from pretty URL to actual one
RewriteRule ^author/([\w-]+)/?$ author.php?name=$1 [L,QSA,NC]
# To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]
# Blog subdomain
RewriteCond %{HTTP_HOST} ^example\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/blog/ [NC]
RewriteRule ^(.*)$ /blog/$1 [L]

Sub-domain Redirect Using htaccess

I am working on a project that requires rewriting a URL like
https://orange.url.com
to
https://www.url.com/?s=orange
I need assistance on how to churn out htaccess to handle this task.
PS: Please not that I already have the following in my htaccess handling some rewriting for me.
RewriteEngine On
RewriteBase /
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R,L]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
You can put this code in your htaccess (which has to be in your root folder)
Options -MultiViews
RewriteEngine On
RewriteBase /
# To internally forward http://xxx.domain.com to http://www.domain.com/?s=xxx
RewriteCond %{HTTP_HOST} ^((?!www\.).+?)\.example\.com$ [NC]
RewriteRule ^$ index.php?s=%1 [L]
# To internally forward http://xxx.domain.com/yyy/ to http://www.domain.com/?s=xxx&c=yyy
RewriteCond %{HTTP_HOST} ^((?!www\.).+?)\.example\.com$ [NC]
RewriteRule ^([^/]+)/$ index.php?s=%1&c=$1 [L]
# To internally forward http://xxx.domain.com/yyy/zzz/ to http://www.domain.com/?s=xxx&c=yyy&g=zzz
RewriteCond %{HTTP_HOST} ^((?!www\.).+?)\.example\.com$ [NC]
RewriteRule ^([^/]+)/([^/]+)/$ index.php?s=%1&c=$1&g=$2 [L]
# To externally redirect /dir/foo.php to /dir/foo/
RewriteCond %{THE_REQUEST} \s/(.+?)\.php [NC]
RewriteRule ^ %1/ [R,L]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.*)$ $1/ [R,L]
# To internally forward /dir/foo/ to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^(.+?)/$ $1.php [L]

Mode Rewrite added another url parameter but not working (multiple parameters)

I have the following htaccess file code below
RewriteEngine On
RewriteBase /site/public/admin/
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
RewriteCond %{THE_REQUEST} \s([^.]+?)(?:\.php)?\?caseid=([^&\s]+) [NC]
RewriteRule ^ %1/caseid/%2/? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/caseid/([^/]+)/?$ $1.php?caseid=$2 [L,NC,QSA]
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R=302,L]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
This works perfectly fine as long as I provide 1 parameter which is caseid i.e
http://localhost/site/public/admin/PIClaimant.php?caseid=11
The URL above successfully changes to
http://localhost/site/public/admin/PIClaimant/caseid/11/
I've had a few pages with two parameters, which are PIClaimant.php?caseid=11&picid=34. Initially when I was creating htaccess file I did not pay attention to those pages (&picid=34 only being used at a few pages 4 or 5 max). Now when I go back to those pages which has those parameters, they are not working not showing the content of page. They work till first parameter PIClaimant/caseid/11/ but not with the second parameter.
Since I am not very good at this (mode rewriting) I need your help.
Any Ideas?
Things are getting more complicated now :)
Try this code in your .htaccess:
RewriteEngine On
RewriteBase /site/public/admin/
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
RewriteCond %{THE_REQUEST} \s([^.]+?)(?:\.php)?\?caseid=([^&\s]+)?\&picid=([^&\s]+)\s [NC]
RewriteRule ^ %1/caseid/%2/picid/%3/? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/caseid/([^/]+)/picid/([^/]+)/?$ $1.php?caseid=$2&picid=$3 [L,NC,QSA]
RewriteCond %{THE_REQUEST} \s([^.]+?)(?:\.php)?\?caseid=([^&\s]+)\s [NC]
RewriteRule ^ %1/caseid/%2/? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/caseid/([^/]+)/?$ $1.php?caseid=$2 [L,NC,QSA]
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} \s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R=302,L,NE]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Change your code to the code below:
RewriteEngine On
RewriteBase /site/public/admin/
RewriteCond %{REQUEST_METHOD} POST
RewriteRule ^ - [L]
#RewriteCond %{THE_REQUEST} \s([^.]+?)(?:\.php)?\?caseid=([^&\s]+) [NC]
RewriteCond %{THE_REQUEST} \s([^.]+?)(?:\.php)?\?caseid=([^&\s]+)?\&picid=([^&\s]+) [NC]
RewriteRule ^ %1/caseid/%2/picid/%3/? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^([^/]+)/caseid/([^/]+)/?$ $1.php?caseid=$2
RewriteRule ^([^/]+)/caseid/([^/]+)/?$ $1.php?caseid=$2&picid=$3 [L,NC,QSA]
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R=302,L]
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
replace the code with your code see if that works

Rewrite index when php is aready removed

I have a code in my htaccess that removes the .php from every page on my site. The following code is as far as i came, but what i actually want is that every page has an slash after the name (still without php)
and that de index(.php) name is completely removed.
Icouldn't find something on the internet having these two combined. I did find a code that created a slash at the end but that created many problems with internally redirecting.
Is all of this possible and if so, how?
Many thanks in advance,
Paul
Options +FollowSymLinks -MultiViews
RewriteEngine On
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
Try:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ /%1/ [R,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)index\.php [NC]
RewriteRule ^ /%1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+?)/?$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule ^(.*?)/?$ /$1.php [L]

Categories