How to remove .php from all urls with no trailing slash? - php

I need to remove the .php from all the urls on my website, and if someone types in a full url it needs to redirect to the php-extension-free version. Eg:
website.com/about.php -> website.com/about
I need to do this in as SEO friendly a manner as possible, so I'm guessing that would be a 301 redirect so Google and others know that the page has a new location at the new php-extension-free URL. Any dup content would also have to be avoided, so it's important that the page isn't accessible at both the old and new urls, which is what would happen if all I did was a simple:
RewriteRule about about.php [L]
I've seen a number of .htaccess approaches for this, but they all seem to add a trailing slash to the URL. It's important that this doesn't happen too.
Also, I have an explicit HTTPS redirect happening for a couple pages, and I need to make sure I don't create a redirect loop. Here's the code that's currently redirecting those pages to HTTPS.
RewriteCond %{HTTPS} off
RewriteRule ^(quote|quote_2).php$ https://website.com/$1.php [R=301,L,QSA]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/(quote|quote_2).php
RewriteCond %{REQUEST_URI} !^/(.*)\.(css|png|js|jpe?g|gif|bmp|woff|svg|map)$
RewriteRule ^(.*)$ http://website.com/$1 [R=301,L,QSA]

You can use this code in your root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
RewriteCond %{HTTPS} off
RewriteRule ^(quote|quote_2)/?$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/(quote|quote_2)
RewriteCond %{REQUEST_URI} !\.(css|png|js|jpe?g|gif|bmp|woff|svg|map)$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

The below answer can be found here
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Related

.htaccess redirect to HTTPS not working

RewriteEngine On
RewriteCond %{SERVER_PORT} ^80$
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI} [L,R]
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]
This is how my current .htaccess looks like at the moment. Line 3 and 4 I took from another question at stackoverflow, and the rest simply removes the .php file extension. But the redirect to https part isn't working. Any ideas?
EDIT:
I just figured out that the problem is in my site's .conf file in the sites-enabled folder of apache. I need a separate block for the :80 and :443 port, which I can't figure out how to do, but that's a topic for another question.
The following is what I use to enforce https, hope it will be of use.
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1 [R,L]
You can try this for redirect on https:
RewriteRule ^(.*)$ https://www.%1.%2/$1 [L,R=301]
The Following code may Works.
Url:- home
RewriteRule ^home$ index.php?page_slug=$0 [QSA]
RewriteRule ^home/$ index.php?page_slug=$0 [QSA]
This is URL- page/new
RewriteRule ^page/([a-zA-Z0-9-/]+)$ page.php?page_slug=$1 [QSA]
RewriteRule ^page/([a-zA-Z0-9-/]+)/$ page.php?page_slug=$1 [QSA]
You can use the following code to redirect from http to https :
RewriteCond %{HTTPS} ^off$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NC,L,R]

htaccess RewriteRule cause loop

please, could you give me an advice, why this .htaccess file cause redirect loop?
I have this url:
www.akomin.cz/ or www.akomin.cz/smth/
and I want to redirect it there:
www.akomin.cz/nedostupny/
But Safari throws me an error, that while trying to open www.akomin.cz/nedostupny/ there were many redirects.
RewriteEngine on
RewriteRule !^/nedostupny/ /nedostupny/ [L,R=302]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(php|html?|jpg|gif)$
RewriteRule ^(.*)([^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Change your first rule to this:
RewriteCond %{THE_REQUEST} !/nedostupny/ [NC]
RewriteRule ^ /nedostupny/ [L,R=302]
You need to use %{THE_REQUEST} because your last rule is changing REQUEST_URI to /index.php hence causing first rule to fire again. THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules.
Make sure to test it after clearing your browser cache.

htaccess redirect domain with query string to subdomain with excludes

We have moved our site to a subdomain while on the main domain we have a new site with new url's. What we are trying to do is having the old url's redirect to the subdomain while the excluding the new url's.
here's examples of the old url's:
http://www.domain.co.il/index.php?dir=app_admin&page=ip_stat&op=list&pos=0
should be redirected to:
http://sub.domain.co.il/index.php?dir=app_admin&page=ip_stat&op=list&pos=0
While the new site url's (do not contain php) and the homepage should not be effected.
This is what our htaccess looks like:
It does redirect the url's to the sub, but does not exclude the new pages and redirect them to the subdomain home (sub.domain.com)
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !contact(|$)
RewriteCond %{REQUEST_URI} !about(|$)
RewriteCond %{REQUEST_URI} index\.php
RewriteRule ^(.*)$ http://sub.domain.com [R=301,L]
Hopefully someone can help us get this right, we get many 404 in our gwt now :/
Thanks for any help !
Fix some regex and reorder your rules:
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} !/(contact|about) [NC]
RewriteCond %{THE_REQUEST} !\.(jpe?g|gif|bmp|png|tiff|css|js) [NC]
RewriteRule ^(.+)$ http://sub.domain.com/$1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
Also test this in a new browser to avoid old browser cache.
Here's the full code for the site.
The way things are set up is that the new site is being called from a folder named "main" while the old site is being called from the root directory.
So we actually have 2 htaccess.
Here's the root htaccess code:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^/([^.]+)\.php
RewriteRule ^(.*)$ http://sub.domain.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} www.domain.com$
RewriteCond %{REQUEST_URI} !^/main
RewriteRule ^(.*)$ /main/$1 [L]
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteRule ^admin$ index.php?dir=app_misc&page=login [L]
RewriteRule ^admin/$ index.php?dir=app_misc&page=login [L]
RewriteRule ^sitemap.xml$ sitemap.php [L]
RewriteRule ^robots.txt$ robots.php [L]
RewriteRule ^adv$ adv/index.php [L]
RewriteRule ^adv/$ adv/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(([0-9]+)_([^/]+)|([0-9]+))$
RewriteRule (.*) /404.php?a=%{REQUEST_URI} [L]
and the htaccess in the "main" folder:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !(contact|about) [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
I've tested on few browsers and in incognito mode as well
Thanks
Instead of excluding urls which caused necessary css & js files to redirect as well, I solved it by creating a redirect that effect only url's with php.
RewriteCond %{THE_REQUEST} ([^.]+)\.php [NC]
RewriteRule ^(.*)$ http://sub.domain.com/$1 [R=301,L,NE]

Codeigniter htaccess to remove index.php and www

I'm trying to get my htaccess rewrite rules to remove the index.php from the url AND also redirect the www. requests to the non-www version.
This is my htaccess which works fine with removing the index.php:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
And I came across another question on how to remove the www part:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
But I just cant seem to get them to play nicely together! Any advice/suggestions most appreciated!
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
RewriteEngine On "starts" rewriting. RewriteCond and RewriteRule work as pairs.
Here, we send user to non-www version first, and cleans URLs.
You first need to make sure that the "non-www" rewrite rule comes first and then the one that will redirect all requests to the CodeIgniter bootstrap file. Do note that the following code will honor only HTTP rewrites. If you also need HTTPS it requires some modifications.
RewriteEngine on
# Sending all www requests to the non-www version.
RewriteCond %{HTTP_HOST} ^www.domain.com [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
# Now the CodeIgniter part
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]

are these rewrite rules correct to archive following things?

i wanted to
redirect all users to domain with WWW
im using codeigniter so wanted to remove index.php from url's
prevent access to index.php and redirect to domain name if someone request domain/index.php
wanted to 301 redirect all pages
so these are the lines i came up with .htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} (.*)$
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ http://%{HTTP_HOST}/$1 [R=301,L]
Are these rules correct to do what i wanted to do? is there better ways to do this?
Regards
I have made some changes and removed some redundant code. Here is your modified .htaccess:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ /%1 [R=301,L]
Also make sure your css, js, images files always use absolute path instead of relative one i.e. path to js, css, images should either start with http:// or /.

Categories