How to add a traling slash - .htaccess

I have the follow code in my htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]{2})/?$ index.php?lang=$1
RewriteRule ^([a-z]{2})/([a-z]+)$ $2.php?lang=$1
How I add a trailing slash, so I can use http://www.liveandletdive.fi/en/contact/ instead of only http://www.liveandletdive.fi/en/contact ?
I figured out, how I can do my site with seo freindly urls, and multilingual. Only this small part remain.

You can use these rules:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
## Adding a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} \s/+(.*?)[^/][?\s]
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([a-z]{2})/?$ index.php?lang=$1 [L,QSA,NC]
RewriteRule ^([a-z]{2})/([a-z]+)/?$ $2.php?lang=$1 [L,QSA,NC]

Related

Configure correct htaccess rules

i use htaccess
Options +FollowSymLinks
RewriteEngine on
# Rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule detalhe/(.*) detalhe.php?id=$1
i want redirect:
https://example.com/detalhe.php?ID=31&URL=lorem-to-lorem
to:
https://example.com/31/lorem-to-lorem.html
any ideias to help?
You may use an additional rule to handle additional URI segment:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/detalhe\.php\?ID=(\d+)&URL=([^\s&]+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(\d+)/(.+)$ detalhe.php?ID=$1&URL=$2 [L,QSA,NC]
RewriteRule ^detalhe/(.*) detalhe.php?id=$1 [L,QSA,NC]

url not rewriting i am using below code and mod_rewrite is also on

My url is like:
http://www.example.com/mock_profile.php?user_name=Yasin
but I want an seo friendly url like:
http://www.example.com/Yasin
I am using the below url RewriteRule in my httaccess but it is not working
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ mock_profile.php?user_name=Yasin
RewriteRule ^([a-zA-Z0-9_-]+)/$ mock_profile.php?user_name=Yasin
## Below is my all httacess code ##
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.html$ $1.php [nc]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
#RewriteRule ^(.*).html$ $1.php [QSA]
RewriteCond %{THE_REQUEST} \ /(.+)\.php
RewriteRule ^ /%1.html [NC,L,R=301]
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9_-]+)/?$ mock_profile.php?user_name=$1
Try this :
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9_-]+)/?$ mock_profile.php?user_name=$1 [L]
Explaination:
The following Condition tells the server to stop rewriting if the Request is for an existing directory.
RewriteCond %{REQUEST_FILENAME} !-d
And this tells the server to stop rewriting if the request is for a valid file:
RewriteCond %{REQUEST_FILENAME} !-f
If the request is not for a file /dir ,then rewrite it :
RewriteRule ^([a-zA-Z0-9_-]+)/?$ mock_profile.php?user_name=$1

Hide directory name from URL

I just want to hide the dir name from URL.
From: example.com/dirname/somepage
To: example.com/somepage
That code doesn't work for me, I have probably made some mistakes
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule / /dir/$1 [L]
I have already this in .htaccess (to hide php extension)
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
This should be your complete .htaccess:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+dirname/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^dirname/)^(.*)$ /dirname/$1 [L,NC]

Mod rewrite remove trailing slash but keep on directories

So far my .htaccess is like this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
<FilesMatch "\.(htm|php|js|css|htc|png|gif|jpe?g|ico|xml|csv|txt|swf|flv|eot|woff|svg|ttf|pdf|gz)$">
RewriteEngine Off
</FilesMatch>
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .*\.html$ index.php [L]
I would like to have url with trailing slash redirected to the one without. But the slash for directories should be kept.
Any help would be very appreciated!
UPDATE:
after some more Google research i assembled some working solution for me. Both rewriting for removing trailing slash and redirect for www are working.
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^rudolfapotheke.de$ [NC]
RewriteRule ^(.*) http://www.%{HTTP_HOST}/$1 [L,R=301]
First, I would change your FilesMatch to a RewriteCond
RewriteCond %{REQUEST_FILENAME} !\.(?:htm|php|js|css|htc|png|gif|jpe?g|ico|xml|csv|txt|swf|flv|eot|woff|svg|ttf|pdf|gz)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
and simplify the pattern for the next RewriteRule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.html$ index.php [L]
or put the two rules together and test just for
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
and finally the rule for removing the trailing slash
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ $1 [R,L]
To redirect those with trailing slash, you need to check if the path is a directory with a RewriteCond and then include a RewriteRule if the filename matchs /$
So it would look something like this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ $1 [R=permanent,L]

Redirect to trailng slash (htaccess)

Trying to add trailing slash to every link. i.e. http://mysite.com/products should make 301 redirect to http://mysite.com/products/ etc. But how? Here is htaccess:
RewriteEngine on
DirectoryIndex index.php
Options -Indexes
RewriteBase /
RewriteCond %{REQUEST_URI} \.css$
RewriteCond %{QUERY_STRING} ^pack$
RewriteRule ^(.*)$ /modules/system/css_compactor.php?filename=$1 [L]
RewriteCond %{REQUEST_URI} \.js$
RewriteCond %{QUERY_STRING} ^pack$
RewriteRule ^(.*)$ /modules/system/js_compactor.php?filename=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php
RewriteRule /admin/(.*)$ /admin/index.php
Need help!
Here's what I'm using
RewriteEngine on
RewriteBase /
### CHECK FOR TRAILING SLASH - Will ignore files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://example.com/$1/ [L,R=301]
Basically, this makes sure that it doesn't add a trailing to file and only folders or paths.
EDIT
To make it domain independent
RewriteRule ^(.*)$ $1/ [L,R=301]

Resources