So I have the following .htaccess file on my server which removes .php extension from all files, forces https and also removes www.:
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]
The problem is that I have an upload form for users to upload an image, so when they upload the images because the file gets redirects from /upload.php to just /upload it then becomes a GET request because of the redirect.
Is there any way I can stop this one file from removing the .php extension?
Just add an extra condition:
RewriteCond %{REQUEST_URI} !/upload\.php$
This condition will prevent your rules to be applied to your upload.php file.
Related
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.
I'm trying to set an htaccess file to remove .php and .html extension, force non-www and https but it's failing on index.php inside subdirectories.
I have the following syntax in htaccess:
RewriteEngine on
# Redirect www and http to https - non-www
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
# Start php extension remove
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
# End php extension remove
# Start html extension remove
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
# End html extension remove
It works perfectly on https and non-www but when I point the en subdirectory (domain.com/en) I get the following message on the browser:
The requested URL /en.php was not found on this server.
Any ideas?
Path to subdirectory would be domain.com/en/ (ended with slash).
domain.com/en is not a directory/file so RewriteRule ^(.*)$ $1.php is true and requested /en.php
I have the following structure:
/
/about.php
/contact.php
/en/
/en/about.php
/en/contact.php
And I want to remove .php extension and www prefix from the urls and force https as well.
Now I have the following htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
Thanks in advance for any help!
You're really close as far as what you have. Now that I know the issue I'll give you what I use -- You need a condition that says is not a directory -- And the syntax is !-d
This is how my htaccess looks (because I use it to remove html as well as php:
RewriteEngine on
# Redirect www and http to https - non-www
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
# Start php extension remove
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
# End php extension remove
# Start html extension remove
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
# End html extension remove
As you can see the first condition checks to see that it's not a directory. The following condition checks to see if it's file extension is .php. If both conditions are true -- The rule is to remove the extension.
As a note -- I would make your syntax a little cleaner and separate the "sections" of what you are trying to do.
UPDATE AS PER COMMENT
To remove the https forcing -- Simply comment out the first condition and change https to http in the rule:
RewriteEngine on
# Redirect www and http to https - non-www
#RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
# Start php extension remove
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
# End php extension remove
# Start html extension remove
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
# End html extension remove
This is my .htaccess code
#RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteCond %{HTTP_HOST} !hashstar\.com$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
#over here we have set the default root directory now request will be directly made from this directory
RewriteCond %{HTTP_HOST} ^hashstar.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^www.hashstar.com$
RewriteCond %{REQUEST_URI} !app/
RewriteRule (.*) /app/$1 [L]
#redirect all requests except only POST
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.(?:php?)[\s?/] [NC]
RewriteRule ^ /%1%2 [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
#we are setting here the default file to load while the URL is called followed by fallback files
DirectoryIndex index.php timer/index.php
My root directory is set to folder app
In app folder, I have 2 folders
1.) Manage
2.) Timer
To access manage folder I have to type in URL
http://www.example.com/manage/
To access timer folder I have to type in URL
http://www.example.com/timer/
if I use these Url by adding a slash in URL (/)
the URL loads perfectly
but if the don't add the (/) in URL then it makes a redirection through the root directory
URL should look like => http://www.example.com/manage
It becomes like => http://www.example.com/app/manage/
What's the reason behind it & how to fix this problem?
Any helps appreciated. Thanks in advance.
Reason of this redirect is that rewritten URI is a real directory without trailing slash. When this happens, Apache's mod_dir module acts on it by redirecting to a URI with a trailing slash.
To prevent use this .htaccess:
#we are setting here the default file to load while the URL is called followed by fallback files
DirectoryIndex index.php
#RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteCond %{HTTP_HOST} !hashstar\.com$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
#redirect all requests except only POST
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.(?:php?)[\s?/] [NC]
RewriteRule ^ /%1%2 [R=301,L,NE]
# adds a trailing directory if rewritten URI is a direcory
RewriteCond %{DOCUMENT_ROOT}/app/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L]
#over here we have set the default root directory now request will be directly made from this directory
RewriteCond %{HTTP_HOST} ^(?:www\.)?hashstar.com$ [NC]
RewriteCond %{REQUEST_URI} !/app/ [NC]
RewriteRule (.*) /app/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
At a glance (I didn't try to reproduce these rules in my Apache2 server), it would seem that this is the culprit:
RewriteRule (.*) /app/$1 [L]
the Regular Expression (.*) will catch Any character greedily. Your Rewrite Rule is simply adding /app/ between the domain and the script.
Find and group <all non-null characters>,
and transform it to /app/<all non-null characters>
The URL www.example.com/myscript.php would be rewritten as www.example.com/app/myscript.php
EDIT
so what code edit do u suggest? #SsJVasto
You could simply exclude / from the greedy rule:
RewriteRule (.*)/ $1 [L]
This way, you are storing the result of anything ending with a /, but without said / into $1. Since the / isn't inside the group, it will be forgotten.
If you want to handle repetition (like http://www.example.com/myscript.php//), you can add a + sign to handle one or more occurrences:
RewriteRule (.*)/+ $1 [L]
I've read many articles and cannot seem to get ALL of the combined .htaccess Rewrites to work together. I either get re-direct loops or one or a few do not work at all.
To be clear I'm looking for the following 5 to occur if needed:
Ensure www on all URLs
Ensure HTTPS for all version of site
Remove index.php from url
Remove all .php extension / Re-direct to url without .php extension
Could be in tandem with the previous: add trailing slash
Some examples:
example.com/index.php => https://www.example.com/
www.example.com/info/really-good-info.php => https://www.example.com/info/really-good-info/
www.example.com/about-us.php => https://www.example.com/about-us/
www.example.com/careers/index.php => https://www.example.com/careers/
Here is current .htaccess setup:
<IfModule mod_rewrite.c>
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /
# Remove all .php extensions without interfering with .js or .css.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)\.(?!js|css)([^.]*)$ $1\.php
# Remove index from url.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
# Ensure www on all URLs.
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
# Ensure we are using HTTPS version of the site.
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Ensure all URLs have a trailing slash.
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ https://www.example.com/$1/ [L,R=301]
</IfModule>
The above .htaccess is ONLY in my root folder and currently does 3 out of the 5 needed: changes to HTTPS, adds www and removes index.php. It does not remove any other .php files extension nor does it add trailing slash.
I see 2 issues:
Redirect rules appearing after rewrite ones
Adding .php should only happen after you ensure corresponding .php file exists.
Have it this way:
Options +SymLinksIfOwnerMatch
RewriteEngine On
RewriteBase /
# Ensure www on all URLs.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=302]
# Ensure we are using HTTPS version of the site.
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302]
RewriteCond %{THE_REQUEST} \s/*(.*?)/index\.php [NC]
RewriteRule ^ %1/ [R=302,L]
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L]
# Ensure all URLs have a trailing slash.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^.]*?[^/.]$ %{REQUEST_URI}/ [L,R=302]
# Remove all .php extensions without interfering with .js or .css.
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]
# Remove index from url.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+?)/?$ index.php?$1 [L,QSA]
Make sure to clear your browser cache before testing this.
Try this to avoid the loop:
#non-www. http to www. https
RewriteCond %{ENV:HTTPS} !on
RewriteCond %{HTTP_HOST} ^(www\.)?yourdomain\.com$
RewriteRule (.*) https://www.yourdomain.com/$1 [R=301,L]
#non-www. https to www. https
RewriteCond %{ENV:HTTPS} on
RewriteCond %{HTTP_HOST} ^yourdomain\.com$
RewriteRule (.*) https://www.yourdomain.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s/*(.*?)/index\.php [NC]
RewriteRule ^ %1/ [R=302,L]
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L]
# Ensure all URLs have a trailing slash.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^[^.]*?[^/.]$ %{REQUEST_URI}/ [L,R=302]
# Remove all .php extensions without interfering with .js or .css.
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]
# Remove index from url.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^.]+?)/?$ index.php?$1 [L,QSA]