htaccess Rewrite (remove php extension) - php

today I have a problem with my .htaccess file. On my server I have several folders, each of folders is separately website example below:
Server structure (global folder)
- my_portfolio
- website_two
- my_website
- example
In "example" folder I have that .htaccess file:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [L]
#To remove www header
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [L,R=301]
RewriteRule working good when I open my website address is without "www" and .php extensions is invisible, but when I click on a link to other website in menu I get something like this:
www.example.com/example/contact.
Address to my website is www.example.com and everything for this website is inside example folder on a server. I dont know where or what I wrote wrong. Always when I try to go on to another webside rewrite engine give me a link without .php extension but add to folder name before link.
This is good example:
www.example.com/contact.php - normal link without htaccess
example.com/example(folder name where is a website)/contact - now
example.com/contact - should be

Reorder your rules and keep redirect rules before internal routing rules:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
#To remove www header
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
Also test this from a new browser to avoid old 301 cache.

Related

htaccess rewrite url php / Godaddy / Linux Hosting

I know that there are too many questions and answers about this so i have to apologize in advanced but nothing works for me.
I lunched my website in Godaddy/Linux Hosting and trying to apply a rewrite for my urls. What it looks like now is
https://www.mywebsite.com/tours/tour_id=15
and i would like it ot be
https://www.mywebsite.com/tour/15 or even better insted of the ID to GET the title from the sql (which if is not possible through htaccess i can do it with php) My Own Title https://www.mywebsite.com/tour/My Own Title
I am really new in htaccess, below is what is already written in the file
# Redirects to https
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?mywebsite\.com
RewriteRule ^(.*)$ https://www.mywebsite.com/$1 [R=301,L]
RewriteEngine On
RewriteBase /
# Removes index.php from ExpressionEngine URLs
RewriteCond %{THE_REQUEST} ^GET.*index\.php [NC]
RewriteCond %{REQUEST_URI} !/system/.* [NC]
RewriteRule (.*?)index\.php/*(.*) /$1$2 [R=301,NE,L]
# Directs all EE web requests through the site index file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
# Remove trailing slash in the end
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
I would be grateful for any help
Add to top of .htaccess this rewrite rule:
RewriteEngine On
RewriteRule ^tour\/\d+$ /tours/tour_id=$1 [L]

Codeigniter remove index.php and prefix the URL with www

I want to remove the default index.php that auto comes with Codeigniter.
I've been able to remove that with this code
RewriteRule ^(.*)$ index.php/$1 [L]
example.com/index.php/blog can now be accessed by example.com/blog
I later wanted to prefix the URL with a www i.e example.com/blog should redirect to www.example.com/blog with these rewrite rules
RewriteCond %{HTTP_HOST} ^(?!www\.)(.+) [NC]
RewriteRule ^(.*) http://www.%1/$1 [R=301,NE,L]
After adding this code above to the end of my .htaccess file, it began to misbehave.
If I enter www.example.com/blog into a URL bar it works fine but if I enter example.com/blog it redirects to www.example.com/index.php/blog
What I'm I doing wrong?
I want example.com/blog to redirect to www.example.com/blog
Note: I am using the Codeigniter framework.
Added: This code is just on top of the previous ones I have up. Maybe this is it's problem please HELP!!!
RewriteCond $1 !^{index\.php|[assests/images/themes/fonts/style/scripts/js/install]|robot\.txt|favicon\.ico}
Try this:
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ example.com/$1 [L,R=301]
After much tricks and following the post given by #Tpojka I was able to come up with this
RewriteEngine On
RewriteBase /
# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
# no www -> www
RewriteCond %{HTTP_HOST} ^(?!www\.)(.+) [NC]
RewriteRule ^(.*) http://www.%1/$1 [R=301,NE,L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
This guy was my problem
RewriteCond $1 !^{index\.php|[assests/images/themes/fonts/style/scripts/js/install]|robot\.txt|favicon\.ico}
I would need help on how to exclude folders and some files like robot.txt file like I tried in the malfunctioning line.
This is the complete .htaccess file which I use personally:
<IfModule mod_rewrite.c>
RewriteEngine On
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 %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

How can I remove .php extension from only one URL in root directory without affecting sub-domains?

I'm trying to hide .php extension from an URL (www.domain.com/home.php) using .htaccess.
The thing is when I use the generic examples I found it disables php for the entire server due to the "home.php" file being located there. Resulting in apps like Wordpress etc to not function correctly located in sub-directories.
You can put this code in your htaccess (which has to be in root folder)
Options -MultiViews
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{THE_REQUEST} \s/([^/]+)\.php(?:\s|\?) [NC]
RewriteRule ^ /%1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f
RewriteRule ^([^/]+)$ /$1.php [L,QSA]

Remove/Redirect index.php from url to prevent duplicate urls

Please read the question carefully before marking as duplicate.
We all know, that using in .htaccess:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
we can redirect all traffic to index.php so we can create friendly urls and have one front controller.
Although the question is connected to mod_rewrite the problem is described for Laravel.
The following .htaccess comes by default with Laravel 4 and it works fine:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
If we run url mydomain.com/something and have set that route for something properly, some controller will be launched. It works fine so far.
However in Laravel 4 we will be able to reach the same route using mydomain.com/index.php/something. Probably using Laravel url creating we will have no urls with index.php in url but there is some other problem.
For example if our competition would like to make us some harm, they can simple put in Internet single links for urls to mydomain.com/index.php/something, mydomain.com/index.php/something2 and so on and search engines will see duplicate urls.
Of course if we have our custom PHP application, we can do it in PHP without a problem checking simply $_SERVER['REQUEST_URI'] and make 301 redirection. We can of course do the same in Laravel but we have to write this code in PHP each time and probably some developers could say it is bad practice to do it in PHP.
Question is simple: how can I redirect in .htaccess all urls that contain index.php to to the same url without index.php?
Example urls that should be redirected:
mydomain.com/index.php/something should be redirected to mydomain.com/something (something could be anything - can contain any characters)
mydomain.com/index.php should be redirected to mydomain.com
mydomain.com/index.php?anything should be redirected to mydomain.com (anything can contain any characters)
mydomain.com/index.phpanything should be redirected to mydomain.com anything can contain any characters)
Insert these rules just below RewriteEngine On line:
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php[^/] /$1? [L,R=302,NC,NE]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php(?:/(.*))?$ /$1$2? [L,R=302,NC,NE]
After spending hours I write below code for me and its 100% working
Redirect index.php to non index.php
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^index.php/(.*)$ /$1 [R=301,L]
how can I redirect in .htaccess all urls that contain index.php to to
the same url without index.php?
Add this to your .htaccess
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
For Nginx, here is the rules :
location / {
rewrite ^/(.*?)index\.php[^/] /$1? redirect;
rewrite ^/(.*?)index\.php(?:/(.*))?$ /$1$2? redirect;
}
This solved my problem to force https & remove index.php from the url in Kohan 2.3
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php[^/] /$1? [L,R=302,NC,NE]
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*?)index\.php(?:/(.*))?$ /$1$2? [L,R=302,NC,NE]
RewriteRule ^(application|system) - [F,L]
RewriteCond %{THE_REQUEST} /index.php [NC]
RewriteRule ^(.*)index\.php$ /$1/ [R=301,L,NC,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?!localhost$|127\.0\.0\.1$)(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

htaccess pretty urls setup

I'm using htaccess for the first time to make pretty urls for my website html files and 1 php file. I was just wondering if I would be able to get some advice on my htaccess file set up and if how I have it set up is a good way? I'd hate for my urls to not work in some situation because of what I have written. :(
Example html file:
before: http://www.domain.com/subdomain/htmlpage.html
after: http://www.domain.com/subdomain/htmlpage/
Single php file:
before: http://www.domain.com/subdomain/phppage.php?p=1
after: http://www.domain.com/subdomain/phppage/1/
I have added in a rule to redirect index.html to index.php. I've also had to add 'base href' in the head of each file because I've used relative links.
the htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^index\.html?$ / [NC,R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*[^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)/$ $1.html [L]
RewriteRule ^(.+)/([0-9]+)/?$ phppage.php?p=$1 [L]
This line
RewriteRule ^(.+)/([0-9]+)/?$ phppage.php?p=$1 [L]
is going to send some pages to phppage.php even though they don't look like
http://www.domain.com/subdomain/phppage/1/
because there is no mention of phppage in the first argument to RewriteRule.
try adding the following to your .htaccess file in the root of your domain
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# redirect http://www.domain.com/subdomain/htmlpage.html to http://www.domain.com/subdomain/htmlpage/
RewriteCond %{REQUEST_URI} ^(/subdomain/[^/]+)\.html$ [NC]
RewriteRule . %1/ [L,R=301]
#redirect http://www.domain.com/subdomain/phppage.php?p=1 to http://www.domain.com/subdomain/phppage/1/
RewriteCond %{REQUEST_URI} ^(/subdomain/[^/]+)\.php$ [NC]
# or alternatively if page is literally phppage uncomment below and comment above
#RewriteCond %{REQUEST_URI} ^(/subdomain/phppage)\.php$ [NC]
RewriteRule . %1/ [L,R=301]
#if url does not end with /
RewriteCond %{REQUEST_URI} ^(/subdomain/.+[^/])$ [NC]
# and its not for an existing file
RewriteCond %{REQUEST_FILENAME} !-f
# put one trailing slash
RewriteRule . %1/ [L,R=301]
#write http://www.domain.com/subdomain/htmlpage/ to htmlpage.html
RewriteCond %{REQUEST_URI} ^/subdomain/([^/]+)/$ [NC]
RewriteRule . %1.html [L]
#write http://www.domain.com/subdomain/phppage/1/ to phppage.php?p=1
RewriteCond %{REQUEST_URI} ^/subdomain/[^/]+/([0-9]+)/$ [NC]
RewriteRule . phppage.php?p=%1 [L]

Categories