I'm trying to remove .php from my URLs and add a trailing slash to the end. I've got the .php removal working just fine, but I'm struggling to get the trailing slash working.
Here's what I've got so far:
# Turn mod_rewrite on
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]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
Thanks in advance!
First of all that code and comments look very familiar :)
Use this code:
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]
Try this one:
# Run PHP without filename extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php
# Return 404 if original request is *.php
RewriteCond %{THE_REQUEST} "^[^ ]* .*?\.php[? ].*$"
RewriteRule .* - [L,R=404]
Related
What I want:
/foo.php display /foo in url bar
/foo redirect to /foo.php
Both must view /foo.php
Must work with /showthread.php?id=00 (/showthread?id=00) and such
I constantly get a redirect loop even if I use [L] everywhere.
I tried these:
(1)
RewriteRule ^/forums$ forums.php [R]
RewriteRule ^/forums\.php$ forums [PT]
(2)
RewriteRule ^forums\.php/?$ forums [L,R=301]
RewriteRule ^forums/?$ forums.php
(3)
RewriteRule ^forums/?$ forums.php [L,R=301]
(4)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([a-z/]+).php$ $1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
Each of the above work for individually foo.php to foo or foo to foo.php but infinite loop on both.
I also tried this:
## don't touch /forum URIs
# RewriteRule ^forums/ - [L,NC]
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
# RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
# RewriteRule ^ %1 [QSA,R,L]
# To internally forward /dir/foo to /dir/foo.php
# RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond %{REQUEST_FILENAME}.php -f
# RewriteRule ^(.*?)/?$ $1.php [QSA,L]
Which works as expected but breaks a lot of site features for some reason. (search bar does not work, forums don't work, login doesn't work)
I need foo.php to still work because MyBB uses it.
Try this code in root .htaccess:
RewriteEngine On
RewriteRule ^forums/ - [L,NC]
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
this rule is working for me but i have a problem
www.domain.com/description_functionhall/117/A%2520S%2520Garden
second rule is taking %2520 so i want that url in this format
www.domain.com/description_functionhall/117/A-S-Garden
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /function_hall_project/
## don't touch /forum URIs
RewriteRule ^forums/ - [L,NC]
## hide .php extension snippet
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?hall_id=([^&\s]+)&booking_date=([^&\s]+)&session=([^&\s]+) [NC]
RewriteRule ^ %1/%2/%3/%4? [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/([^/]+)/([^/]+)/([^/]+)/?$ $1.php?hall_id=$2&booking_date=$3&session=$4 [L,QSA]
# To externally redirect /dir/foo.php?hall_id=123&name=abcd to /dir/foo/123/abcd
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?hall_id=([^&\s]+)&name=([^&\s]+) [NC]
RewriteRule ^ %1/%2/%3? [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/([^/]+)/([^/]+)/?$ $1.php?hall_id=$2&name=$3 [L,QSA]
# To externally redirect /dir/foo.php?id=123 to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?hall_id=([^&\s]+) [NC]
RewriteRule ^ %1/%2/? [R,L]
# To internally forward /dir/foo/12 to /dir/foo.php?id=12
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/([^/]+)/?$ $1.php?hall_id=$2 [L,QSA]
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\s [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*?)/?$ $1.php [L]
You can insert this rule just below ## hide .php extension snippet line to convert all these white spaces to hyhen:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.*?)(?:\+|%2520|%20|\s)+(.+?)\sHTTP [NC]
RewriteRule ^ /%1-%2 [L,NE,R=302]
i have one url in my project like
hall/description_banquethall.php?hall_id=1 and my url is working like hall/description_banquethall/1
by using this .htaccess code
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /hall
## don't touch /forum URIs
RewriteRule ^forums/ - [L,NC]
## hide .php extension snippet
# To externally redirect /dir/foo.php?id=123 to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?hall_id=([^&\s]+) [NC]
RewriteRule ^ %1/%2/? [R,L]
# To internally forward /dir/foo/12 to /dir/foo.php?id=12
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/([^/]+)/?$ $1.php?hall_id=$2 [L,QSA]
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\s [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*?)/?$ $1.php [L]
now i want to redirect my another url hall/booking.php?hall_id=1&booking_date=2014-10-23&session=Morning to hall/booking/1/2014-10-23/Morning
Below ## hide .php extension snippet line add these 2 new rules:
RewriteCond %{THE_REQUEST} ^GET\s([^.]+)\.php\?hall_id=([^&\s]+)&booking_date=([^&\s]+)&session=([^&\s]+) [NC]
RewriteRule ^ %1/%2/%3/%4? [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/([^/]+)/([^/]+)/([^/]+)/?$ $1.php?hall_id=$2&booking_date=$3&session=$4 [L,QSA]
I have the following in my htaccess
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
How do I append a trailing slash to the URL using the above code snippet?
In response to your 1st question item, you can use the follow to add the trailing slash:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]
Basically what it does is, it checks if the php file referenced to that address exists, if it does and the URL does not have a ending slash it adds it.
The full code would look like this:
RewriteEngine On
RewriteBase /
# 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} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
I have a code in my htaccess that removes the .php from every page on my site. The following code is as far as i came, but what i actually want is that every page has an slash after the name (still without php)
and that de index(.php) name is completely removed.
Icouldn't find something on the internet having these two combined. I did find a code that created a slash at the end but that created many problems with internally redirecting.
Is all of this possible and if so, how?
Many thanks in advance,
Paul
Options +FollowSymLinks -MultiViews
RewriteEngine On
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
Try:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ /%1/ [R,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)index\.php [NC]
RewriteRule ^ /%1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+?)/?$
RewriteCond %{DOCUMENT_ROOT}%1.php -f
RewriteRule ^(.*?)/?$ /$1.php [L]