I've been trying to delete the .php extension and force trailing slashes in the end of the URLs.
I've found an answer to do it exactly what I need at (.htaccess trouble with hiding file extension and forcing trailing slash) but it does not work, I believe it's due to how I am forcing my website to change the document root.
As I do not have access to Virtual Hosts I must change my document root using htaccess, and I have accomplished it by applying the following code..
RewriteCond %{HTTP_HOST} ^site.co$ [NC]
RewriteRule ^(.*)$ http://www.site.co/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !webroot/
RewriteRule (.*) /webroot/$1 [L]
The only working solution that I've been able to apply to hide the .php extension is with the code below.
removing .php extension(only working solution)
RewriteCond %{THE_REQUEST} ^\w+\ /(.*)\.php(\?.*)?\ HTTP/
RewriteRule ^ http://%{HTTP_HOST}/%1 [R=301]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .* $0.php
This code is in the thread that I have linked above, but it does not work on my website as I believe it's due to the document root.
RewriteEngine on
# Forces a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?[^/])$ /$1/ [R=301,L]
# .php ext hiding
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]
Try these modified rules with different ordering:
RewriteEngine on
# add www before hostname
RewriteCond %{HTTP_HOST} ^site\.co$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE]
# if on article page, get slugs and make into friendly url
RewriteCond %{THE_REQUEST} \s/article\.php\?article_uid=([^&]+)&article_title=([^&\ ]+)
RewriteRule ^ /article/%1/%2/? [L,R=302,NE]
# if page with .php is requested then remove the extension
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L,NE]
# Force a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s/+([^.]+?[^/.])[\s?] [NC]
RewriteRule ^ /%1/ [R=302,L]
# allow page direction to change the slugs into friendly seo URL
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?:^|/)article/([^/]+)/([^/]+)/?$ /webroot/article.php?article_uid=$1&article_title=$2 [L,QSA,NC]
# silently rewrite to webroot
RewriteCond %{REQUEST_URI} !/webroot/ [NC]
RewriteRule ^ /webroot%{REQUEST_URI} [L]
# .php ext hiding
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Related
I want to redirect all common forms of an index url.
My .htaccess file looks like this right now:
## Mod_rewrite in use.
RewriteEngine On
# Redirect .html URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} \.html$
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
With my knowledge I created this expression:
## Mod_rewrite in use.
RewriteEngine On
# Redirect index
RewriteCond %{REQUEST_URI} ^(.*)index(\.php|\.html?)?$ [NC]
RewriteRule ^(.*)$ / [R=301,L]
# Redirect .html URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} \.html$ [NC]
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
And it doesn't work.
This are my questions:
Why can't I use the NC flag there?
RewriteCond %{REQUEST_URI} \.html$ [NC]
When I use RewriteRule in combination with RewriteCond, what is RewriteRule selecting.
Sample URL: https://analytics.google.com/analytics/web/
This is the condition:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} \.html$
In my use case is regex selecting the part between string start and .html. But whats the string start?
Why is my first Condition & Rule not working?
I want to select all this urls:
.com/index.html
.com/index.htm
.com/index.php
.com/index
Thx for the support.
I need help with the following .htaccess, i need to combine them together if its possible
This will redirect to the domainname.com.au with http://www at the front of domain name if there is no http:// or www at the front:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domainname.com.au$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domainname.com.au$
RewriteRule ^(.*)$ "http\:\/\/www\.domainname.com.au\/$1" [R=301, OR]
And this one should get the variables that passing at the end of the domain name after slash /, I should see "/contact" but the php MVC see this "/index.php?url=contact"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
deny from 111.93.163.217
Thanks!
The two rules can't be combined as they're quite different, but they can be shortened:
# Force www
RewriteCOnd %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Rewrite parameter
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [B,NE,QSA,L]
i am trying to remove .php .html and traling slash from url using htaccess,how ever this code working fine for php but not for html and trailing slash
For PHP
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]
For HTML
RewriteCond %{REQUEST_URI} index.html
RewriteRule ^(.*)index.html$ /$1/ [R=301,L]
Have it this way:
# externally redirect /dir/file.php to /dir/file and remove index
RewriteCond %{THE_REQUEST} \s/+(.*?/)?(?:index)?(.*?)\.(?:html?|php)/?[\s?] [NC]
RewriteRule ^ /%1%2 [R=301,L,NE]
# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /$1 [R=301,L]
# internally add php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# internally add html extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ $1.html [L]
The following regex works perfectly except for under the condition when the user goes to examplesite1.com/site1. When this happens the page fails because of multiple redirects in which it continues to add the request=site1 query to the URL. I thought that adding RewriteCond %{ENV:REDIRECT_STATUS} ^$ would fix this issue however that seemed to not be the case.
This setup is for a multi-domain website. There are three folders site1, site2, and shared. There are also rewrite rules for site2 and shared that are not shown as they do not effect this issue.
# if the resource exists in site1 return it to the user
RewriteCond %{HTTP_HOST} examplesite1.com$
RewriteCond %{DOCUMENT_ROOT}/site1/%{REQUEST_URI} -f
RewriteRule ^(.*)$ /site1/$1 [QSA,L]
# no file found, send URI to CMS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?request=$1 [QSA,L]
complete .htaccess file:
AddDefaultCharset UTF-8
# Disallow browsing file directories
Options -Indexes
RewriteEngine on
RewriteBase /
#remove the trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.+)/$ /$1 [R=301,L]
RewriteCond %{HTTP_HOST} examplesite1.com$
RewriteCond %{DOCUMENT_ROOT}/site1/%{REQUEST_URI} -f
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)$ /site1/$1 [QSA,L]
RewriteCond %{HTTP_HOST} examplesite2.com$
RewriteCond %{DOCUMENT_ROOT}/site2/%{REQUEST_URI} -f
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)$ /site2/$1 [QSA,L]
RewriteCond %{DOCUMENT_ROOT}/shared/%{REQUEST_URI} -f
RewriteRule ^(.*)$ /shared/$1 [QSA,L]
# resources not to be public are sent to CMS
RewriteCond %{REQUEST_FILENAME} (\.inc\.|\.tpl$)
RewriteRule ^(.*)$ /index.php?request=$1 [QSA,L]
# no resource found, send URI to CMS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)$ /index.php?request=$1 [QSA,L]
Try adding an extra condition:
RewriteCond %{HTTP_HOST} examplesite1.com$
RewriteCond %{REQUEST_URI} !^/site1/
RewriteCond %{DOCUMENT_ROOT}/site1/%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/site1/%{REQUEST_URI} -d
RewriteRule ^(.*)$ /site1/$1 [QSA,L]
What looks like may be happening here is that you need to prevent a redirect from happening after one of your other rules get applied. Try adding this right below RewriteBase /:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
I have been trying to get my urls re-written. The first 4 rules are vital, but they are clashing with this line: (i think).
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^.#?\ ]+)\.php([#?][^\ ]*)?\ HTTP/
this stops the url being able to be accessed like so www.example.com/page.php and redirects to www.example.com/page/
after adding the first 4 rules you can see in the htaccess, the above condition doesnt seem to work and Im able to access urls like this :(
lovelakedistrict.com/lake-district-cottages.php/cottages/5/
obviously i should be like this
lovelakedistrict.com/lake-district-cottages/cottages/5/
however this still works :)
lovelakedistrict.com/lake-district-cottages.php -->
lovelakedistrict.com/lake-district-cottages/
This is my htaccess file- can anyone see what happening?
Options +FollowSymlinks
Options +Indexes
RewriteEngine on
#these 4 rules stop being able to access pages like eg
#/lake-district-cottages/?cottages=5/
#/lake-district-cottages/?cottages/5/ --> all direct to /lake-district-cottages/cottages/5/
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?[^\ ]+
RewriteCond %{QUERY_STRING} ^/*([^=&]*[^=&/])/*(&+(.*))$
RewriteRule ^(.*[^/])/?$ /$1/%1/?%3 [N]
RewriteCond %{REQUEST_URI} !^/result
RewriteCond %{REQUEST_URI} !^/shop-result
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?[^\ ]+
RewriteCond %{QUERY_STRING} ^/*([^=&]*[^=&/])/*$
RewriteRule ^(.*[^/])/?$ /$1/%1/?%4 [L,R=301]
RewriteCond %{REQUEST_URI} !^/result
RewriteCond %{REQUEST_URI} !^/shop-result
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?[^\ ]+
RewriteCond %{QUERY_STRING} ^/*([^=&]*[^=&/])/*=/*([^&]*[^&/])/*(&+(.*))$
RewriteRule ^(.*[^/])/?$ /$1/%1/%2/?%4 [N]
RewriteCond %{REQUEST_URI} !^/result
RewriteCond %{REQUEST_URI} !^/shop-result
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]*\?[^\ ]+
RewriteCond %{QUERY_STRING} ^/*([^=&]*[^=&/])/*=/*([^&]*[^&/])/*$
RewriteRule ^(.*[^/])/?$ /$1/%1/%2/? [L,R=301]
RewriteCond %{REQUEST_URI} !^/result
RewriteCond %{REQUEST_URI} !^/shop-result
# this stops you accessing pages with php extention eg
#/lake-district-cottages.php --> directs to /lake-district-cottages/
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^.#?\ ]+)\.php([#?][^\ ]*)?\ HTTP/
# this ignors the include folder and does not add trailing slash (so ajax file works)
RewriteCond %1 !^include/
RewriteRule ^([^.]+)\.php$ /$1 [R=301,L]
#this removes php extention
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
#this removes php extention and adds trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ $1.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
#these re-write urls allowing / to replace ? and =
RewriteRule ^lake-district-cottages/cottages/([0-9]+) lake-district-cottages.php?cottages=$1
RewriteRule ^lake-district-hotels/hotels/([0-9]+) lake-district-hotels.php?hotels=$1
RewriteRule ^lake-district-bed-and-breakfast/bed-and-breakfast/([0-9]+) lake-district-bed-and-breakfast.php?bed-and-breakfast=$1
RewriteRule ^lake-district-lodges/lodges/([0-9]+) lake-district-lodges.php?lodges=$1
RewriteRule ^lake-district-cottages-shop/lake-district-book-shop/([0-9]+) lake-district-cottages-shop.php?lake-district-book-shop=$1
RewriteRule ^lake-district-lodges/lodges/([0-9]+) lake-district-lodges.php?lodges=$1
RewriteRule ^result/([a-zA-Z0-9])/([0-9]+) result.php?$1=$2
RewriteRule ^shop-result/([a-zA-Z0-9])/([0-9]+) shop-result.php?$1=$2
The condition pattern doesn't match your URLs that contain PATH_INFO, but simplifying it a bit to something like this should take care of that:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^.?\ ]+)\.php
RewriteCond %1 !^include/
RewriteRule ^([^.]+)\.php(/.+)?$ /$1%{PATH_INFO} [R=301,L]