.htaccess: Redirect Trailing Slashes - apache

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]

Related

How to remove md5 from the filename in the requested URL for the file name without md5 using apache mod_rewrite?

Example:
This => URL request ./any_folder/main.d25a1b054a0b0fbeb3def5a0ff50d01e.min.js
For this => URL request ./any_folder/main.min.js
My htaccess:
RewriteEngine On
# Remove md5 from asset files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+).([a-fA-F0-9]{32}).(.+)?(js|css)$ $1.$3 [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
The above configuration does not work.
Have your htaccess rules file following manner. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Remove md5 from asset files
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
##RewriteRule ^([^.]*)\.(?:[[:alnum:]]{32})\.([^.]*)\.(js|css)/?$ $1.$2.$3 [L]
##Since following has worked for OP, so adding it here, commenting above.
RewriteRule ^(.+).([a-fA-F0-9]{32}).(.+)?(js|css)$ $1.$4 [L]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

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.

Force trailing slash and 301 redirect in existing RewriteRule

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]

htaccess: redirect of index.php

Sorry for not-very-clear title.
I'm using FatFreeFramework with his .htaccess:
RewriteCond %{REQUEST_URI} \.ini$
RewriteRule \.ini$ - [R=404]
#RewriteCond %{REQUEST_URI} \.html?$
#RewriteRule \.html?$ - [R=404]
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L,QSA,E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
Now, restyling an old site, I have in Google some url codified like
www.example.com/index.php?ID=12
I would strip away that and redirect them to home page.
I don't know how write the rule, I did try to edit in this mode but without luck:
RewriteCond %{REQUEST_FILENAME} !-d
Redirect 301 /index.php?ID=$1 index.php
RewriteRule .* index.php [L,QSA,E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
Right above this rules:
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L,QSA,E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
Add this:
RewriteCond %{THE_REQUEST} \ /+index\.php\?ID=[0-9]
RewriteRule ^ / [L,R=301]
You can't match against the query string using mod_alias's Redirect directive.

htaccess redirect to subfolder with a trailing slash

I successfully managed to redirect all requests to a subfolder of my web server.
This is the code I'm using:
/.htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /public/$1 [L]
/public/.htaccess
# redirect all urls that doesn’t have a
# trailing slash to urls with a trailing slash
# (this is my try)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1 [NC]
It works perfectly, so the following redirects work:
example.com -> example.com/public/
example.com/foo/ -> example.com/public/foo/
example.com/foo/about.html -> example.com/public/foo/about.html
By the way, the following redirect has a problem:
example.com/foo -> example.com/public/foo
Although it redirects correctly, the actual URL in the address bar is:
example.com/public/foo/
So my question is:
How can I have a clean URL when redirecting from a URL without a forward slash?
Hope it makes sense.
Place this code in root .htaccess:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /public/$1/ [L]
Then just remove /public/.htaccess since that is not needed.