apply slash at the end of the URL with htaccess conditions - .htaccess

I need help here.
I want apply a slash at the end of my urls with htaccess, but unfortunately its not working this way, maybe you guys can help me here?
This is my htaccess:
RewriteEngine on
RewriteBase /
## hide .php extension except in POST method
## To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{REQUEST_METHOD} !POST [NC]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
I tried add this rule, but do not work
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]
I appreciate any help here.
Thanks :)

Related

Remove "/php/" subdirectory from URL with .htaccess

I've been trying to achieve the following rewrite/redirect on a website I'm building but I'm struggling and can't find the right answer online.
I want to rewrite
http://www.example.com/php/someFile.php
To:
example.com/someFile
I have managed to achieve most of the desired result with the following code in my .htaccess root file but I need help with removing the php subdirectory:
RewriteEngine on
# remove www from url
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301,NC]
# redirect /file.php to /file
RewriteCond %{THE_REQUEST} \s/([^.]+)\.php [NC]
RewriteRule ^ /%1 [NE,L,R]
# internally map /file to /file.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)/?$ /$1.php [L]
Any help would be hugely appreciated.
Have it this way in your site root .htaccess:
RewriteEngine on
# remove www from url
RewriteCond %{HTTP_HOST} ^www\.(example\.com)$ [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# redirect /php/file.php to /file
RewriteCond %{THE_REQUEST} \s/+(?:php/)?([^.]+)\.php [NC]
RewriteRule ^ /%1 [NE,L,R=301]
# internally map /file to /php/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/php/$1.php -f
RewriteRule ^(.+?)/?$ php/$1.php [L]
Make sure to test after clearing your browser cache.
With your shown samples, could you please try following.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine on
# remove www from url
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^ http://example.com%{REQUEST_URI} [L,R=301,NE]
# redirect /file.php to /file
RewriteCond %{THE_REQUEST} \s/php/file\.php\s [NC]
RewriteRule ^ file [R=301]
RewriteRule ^ php/file.php [L]

Get Rewrite rules to be ignored on specific URL's

I have re-created a site from WordPress in native PHP, and have added certain ReWrite rules in the .htaccess to mimic the WP URL's, but I need to add a couple of reWrite rules to beautify a couple of URL's.
I tried creating 2 directories and having separate .htaccess files in each directory, but to no avail.
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]
#Beautify new URL's
RewriteRule ^operating-areas/([^/]+) town.php?place=$1
RewriteRule ^operating-area/([^/]+)/([^/]+) keyword.php?place=$1&keyword=$2
www.domain.com/operating-areas/London
needs to be sent to
myPage.php?place=London
and then
www.domain.com/operating-area/London/Finance
needs to be sent to
myOtherPage.php?place=London&keyword=Finance
At the moment I am getting 'Too many redirections'.
The rules I have used for the WordPress styling were found on the internet, hence my limited knowledge of this subject hinders me from stopping them firing in specific circumstances.
You can replace all of your code with this block:
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]
#Beautify new URL's
RewriteRule ^operating-areas/([^/]+)/?$ town.php?place=$1 [L,QSA,NC]
RewriteRule ^operating-area/([^/]+)/([^/]+)/?$ keyword.php?place=$1&keyword=$2 [L,QSA,NC]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
/?$ at the end of the pattern allows matching of an optional / at the end. It is also important to keep this rule before .php adding rule.

Htaccess doesn´t work - nice url

Hi guys i have problem with a htaccess.
I want from this URL domain.com/index?article=28&title=velky-biel-karlova-ves
(it doesn´t have index.php because i hide .php extension with htaccess.
I wanna have this URL domain.com/clanok/28/velky-biel-karlova-ves
I used this code
My htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^clanok/([\d]+)/([\w-]+)/?$ index.php?article=$1&title=$2 [NC,L]
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
My index.php file:
<a href="index.php?article=<?php echo urlencode($row['id']); ?>&title=<?php echo createSlug($row['title']); ?>" title="">
It doesn´t work.
Thx for help.
Try it like this you still have to mention index.php your php hide rule is different from this,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^clanok/([\d]+)/([\w-]+)/?$ index.php?article=$1&title=$2 [NC,L]
Please ensure this rule is not conflicting with your existing rules.

How externally redirect url using htaccess, when url contain space, number or dash

I am new in htacces.
I want to create htaccess like stackoverflow.
Check any url of stackoverflow like "hide file extension in url by htaccess". If you put .html/.php/.asp/.abc/.xyz anything it will redirect to "hide file extension in url by htaccess" only even you put / at last it has no effect
Means I want to say url filename contain any keyboard character and it will redirect externally.
below are my current htaccess
RewriteEngine on
# To internally
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
# To externally redirect /dir/file.html to /dir/file
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ %1 [R=301,L,NE]
You need to use appropriate ``RewriteBase`:
RewriteEngine on
RewriteBase /folder1/folder2/
# To externally redirect /dir/file.html to /dir/file
RewriteCond %{THE_REQUEST} ^GET\s(.+?)\.html[\s?] [NC]
RewriteRule ^ %1 [R=301,L,NE]
# To internally
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.+?)/?$ $1.html [L]
My final htaccess file with the help of great #anubhava
RewriteEngine on
RewriteBase /folder1/folder2/
# To externally redirect /folder1/folder2/file.html to /folder1/folder2/file.html or any extension like .php/.asp/.abcd etc
RewriteCond %{THE_REQUEST} ^GET\s(.+?)\.[a-z0-9]{2,}[\s?] [NC]
RewriteRule ^ %1 [R=301,L,NE]
# To internally
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

Excluding a file or directory from .htaccess rewrite

You had a great response that helped me resolve my .htaccess issue on GoDaddy. This was the code you provided:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
QUESTION: How can I exclude a file and/or directory from being rewritten?
Thanks!
David
Assuming you don't want to redirect /ignore.php. Modify above code like this:
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteCond %{REQUEST_URI} !^/ignore\.php$ [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !^/ignore$ [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]

Resources