Hide .php extension in ALL subdirectories with .htaccess - .htaccess

I am trying to hide the .php extensions on my page for all subdirectories with .htaccess.
Currently I have
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]
It works perfectly fine with normal pages, however any page inside a folder gives me a 500 Internal Service Error. An example URL would be
http://www.motorcityline.com/portfolio/michigantinting
Which is actually /portfolio/michigantinting.php. The .php file does exist, I just don't have the .htaccess file configured properly.

Replace your .htaccess with this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# add www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=301,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]

Try this :
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://www.%{SERVER_NAME}%{REQUEST_URI} [R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /$1.php [L]

Related

HTACCESS Stopped working for removing page extensions

I have been using htaccess for performing the following functions:
Redirect to https version
Add WWW. if the request is without www
Remove .php extensions from all pages
Add tailing slash to all pages (eg. /page/)
The code I have is using is:
RewriteRule ^(directory1|directory2)($|/) - [L]
ErrorDocument 404 /404/
#Remove Php extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
#Add Tailing Slash
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
#Add WWW and https
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)$ [NC]
RewriteRule (.*) https://www.%1/$1 [R=301,L]
#SEO INDEX FIX
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ https://www.example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} ^.*/index.html
RewriteRule ^(.*)index.html$ https://www.example.com/$1 [R=301,L]
Everything used to work fine before but now after I upgraded to php 7.4 if I try to access page using .php extension its not removing .php and adding /.
Please help me correct the htaccess code..
Thanks!
You can use this htaccess file :
RewriteEngine On
#Add WWW and https
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule (.*) https://www.%1/$1 [R=301,L]
#Add a trailing slash to pages
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !/$ %{REQUEST_URI}/ [R,L]
#Remove .PHP from URLs
#The rule bellow redirects URLs to remove .PHP (file.php =>file)
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1/ [L,R]
#The rule bellow maps non PHP URLs to PHP
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ /$1.php [L]
Make sure to clear your browser cache or use a different browser to test these rules.

My .htaccess code for apache not working right. Redirection is wrong

AddDefaultCharset UTF-8
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,NC,L]
RewriteRule ^(.*)/$ ./msadmin/index.php?string=$1 [NC,L]
after writeing https://example.com/msadmin/ redirecting to
https://example.com/msadmin/index.php?string=https://example.com/msadmin
which part is wrong?
I need to go to https://example.com/msadmin/
Thanks
Replace all of your code with this:
AddDefaultCharset UTF-8
RewriteEngine On
RewriteBase /
## add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)/?$ msadmin/index.php?string=$1 [QSA,L]
Make sure to test it in a new browser.

htaccess can not remove index.php

I used this code to force www and remove index.php:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?id=$1 [L]
RewriteCond %{THE_REQUEST} ^GET\ /(.*/)?(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /%1%2? [R=301,L]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
It works on centOS server. but not in Ubuntu 16.
It can force www, but it does not remove index.php.
Also I want to redirect example.org to example.com.
There's no good reason why it would part work on one OS and not the other. Except if previous (erroneous) redirects had been cached.
However, your directives are in the wrong order. The internal rewrites (front controller) needs to come after the canoncial redirects, otherwise it will never "force www" or "remove index.php" when requesting a "virtual URL".
For example:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{THE_REQUEST} ^GET\ /(.*/)?(?:index\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ /%1%2? [R=301,L]
# Front controller should be after canonical redirects
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?id=$1 [L]
also I want redirect example.org to example.com
Add the following before the above redirects:
RewriteCond %{HTTP_HOST} ^(www\.)?example\.org [NC]
RewriteRule ^ https://example.com%{REQUEST_URI} [R,L]
You will need to clear your browser cache before testing.

custom url in php using htaccess (url rewriting)

i am new in url rewriting in php htaccess. currently my url as like http://localhost/htaccess/Movie/my-parameter-value
but i want to http://localhost/htaccess/my-parameter-value i want remove Movie (movies is from action source name ex "movie.php");
Thanks In Advance
here is my code :
#Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*?)(?:\+|%20|\s)+(.+?)\sHTTP [NC]
RewriteRule ^ /%1-%2 [L,NE,R=302]
# To externally redirect /dir/foo.php?name=123 to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?name=([^&\s]+) [NC]
RewriteRule ^ %1/%2? [R,L]
# To internally forward /dir/foo/12 to /dir/foo.php?name=12
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/([^/]+)?$ $1.php?name=$2 [L,QSA]
Have it like this:
#Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*?)(?:\+|%20|\s)+(.+?)\sHTTP [NC]
RewriteRule ^ /%1-%2 [L,NE,R=302]
# To externally redirect /dir/foo.php?name=123 to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s(.*)/Movie\.php\?name=([^&\s]+) [NC]
RewriteRule ^ %1/%2? [R,L]
# /htaccess/Movie.php?name=movie-name => /htaccess/movie-name
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?name=([^&\s]+) [NC]
RewriteRule ^ %1/%2? [R,L]
# To internally forward /dir/foo/12 to /dir/foo.php?name=12
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/([^/]+)/?$ $1.php?name=$2 [L,QSA]
# handle /htaccess/movie-name internally
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ Movie.php?name=$1 [L,QSA]

Force https:// for some URLs, and force www. on all

I'm trying to set up an SSL for a site using Codeigniter.
I need to to force https:// on certain pages (such as login, basket etc).
What I have so far, which forces www. successfully, and removes the index.php from the URL for cleaner results with Codeigniter:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1
RewriteCond %{HTTP_HOST} ^mydomain\.com [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Keep your redirect rules before rewrite ones:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain\.com [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{SERVER_PORT} !=443 [OR]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{THE_REQUEST} /(login|basket|shop)
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]

Resources