Force trailing slash and 301 redirect in existing RewriteRule - apache

I have a rewrite rule that shows the contents of a directory. For example, if I go to https://www.example.com/bob/, it will show the contents of the file https://www.example.com/profile/index.php?username=bob
Here is the code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/$ profile/index.php?username=$1 [QSA,L]
But how can I 301 redirect https://www.example.com/bob to https://www.example.com/bob/ (force trailing slash)?

You can place this rule before above rule to force a trailing slash:
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]

Related

I need to remove language code from URL using htaccess

I have two language /ru and /uk. /uk is the default.
I need to remove /uk from the URL.
For example:
www.example.com.ua/uk/category
to
www.example.com.ua/category
But any URL with /ru must not change. ie. www.example.com.ua/ru/category.
htaccess look like
DirectoryIndex index.html index.php
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^init.php$ - [F,L,NC]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(favicon|apple-touch-icon-|homescreen-|firefox-icon-|coast-icon-|mstile-).*\.(png|ico)$ - [R=404,NC,L]
RewriteCond %{REQUEST_URI} ^api/(.*)$ [or]
RewriteCond %{REQUEST_URI} .*/api/(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .*api/(.*)$ api.php?_d=$1 [L,QSA]
RewriteCond %{REQUEST_URI} \.(png|gif|ico|swf|jpe?g|js|css|ttf|svg|eot|woff|yml|xml)$ [NC,or]
RewriteCond %{REQUEST_URI} store_closed.html$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*?)\/(.*)$ $2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
In order to remove the /uk path segment from the start of the URL-path then you would need to do something like the following at the top of your .htaccess file:
RewriteRule ^uk/(.*) /$1 [R=302,L]
This does assume that you have already modified all your internal links and canonical tags to remove the /uk path segment.
If this is intended to be permanent then change the 302 (temporary) redirect to 301 (permanent) only once you have confirmed that everything works as intended, so as to avoid potential caching issues.

Redirect to index.php and remove trailing slash .htaccess

There appears to be many questions like this here but can't find an answer that fits my current code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ public/index.php [QSA,L]
This redirects everything to public/index.php which works fine but I want to get rid of the trailing slash so that example.com/foo/ becomes example.com/foo
You can insert a trailing slash removal code before your current rule:
RewriteEngine On
## Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/+$
RewriteRule ^ %1 [R=301,NE,L]
## forward all requests to public/index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ public/index.php [L]

.htaccess: Redirect Trailing Slashes

I have a rewrite rule below in my .htaccess file in the root directory:
RewriteEngine On
# Exclude .css / .js / ...
RewriteCond ${REQUEST_URI} ^.+$
RewriteCond %{REQUEST_FILENAME} \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml)$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
# Rewrite Rule
RewriteRule ^([^/]*)/([^/]*)$ /index.php?category=$1&product=$2
So the URL http://example.com/data1/data2 give me category=data1 and product=data2.
The problem is that the below URLs are not working:
http://example.com/data1 # Not Working (Page Not Found)
http://example.com/data1/data2/ # Not Working (Page Not Found)
But these URLs are working:
http://example.com/data1/ # Works -> category=data1
http://example.com/data1/data2 # Works -> category=data1 & product=data2
How can I redirect the first two URLs to the second one?
OR/AND
Do something that all URLs are redirect to the non-trailing slash one. So the URLs below:
http://example.com/data1/
http://example.com/data1/data2/
are redirect to these:
http://example.com/data1
http://example.com/data1/data2
To avoid trailing slashes alltogether, do a redirect
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R,L]
To match a URL with just one element, you might use a second rule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php?category=$1&product= [L]
or
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php?category=$1 [L]
Putting all together gives
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R,L]
RewriteRule ^(.+?)/(.+)$ /index.php?category=$1&product=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php?category=$1 [L]

htaccess, append a forward slash, then remove and process without altering the URL

I would like to rewrite all urls from say, www.example.com to www.example.com/
I have a rule that adds the forward slash:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
I would like to create another rule. It's purpose is to check if a trailing slash is present. If so, it then removes it for processing without altering the URL shown in the browser.
Can this be done?
You can use these 2 rules for that:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} \s/+(.*?)[^/][?\s]
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ /$1 [L]

Tricky 301Redirect Rule

I want link to be redirected in a manner such that url with not slash at the end should be redirected to the url with the slash at the end.
example 1:
http://example.com/quiz/funny-riddles-with-answers
should be redirect to
http://example.com/quiz/funny-riddles-with-answers/
example 2
http://example.com/quiz/math-riddles-with-answers
should be redirected to
I have no idea how to do it.
my current screen shot of .htacess is
RewriteEngine On
RewriteBase /quiz/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)?$ mypage.php?param1=$1&param2=$2 [L,QSA]
Try these rules in /quiz/.htaccess:
RewriteEngine On
RewriteBase /quiz/
# add a trailing slash for non-directories
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?[^/])[?\s]
RewriteRule ^ %1/ [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ mypage.php?param1=$1&param2=$2 [L,QSA]
You can easily rewrite to a trailing slash using:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
Place above your already existing rewrites.
You need to change your current rule's pattern to: ^([^/]+)/([^/]+)?/?$
then add this right under RewriteBase /quiz/:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ /$1/$2/ [L,R=301]