I need to remove language code from URL using htaccess - apache

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.

Related

.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: 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.

Transparently rewrite/split specific url to different subfolder

I need to transparently split traffic to two different subdirectories.
For example any url with /api/...
Needs to be rewritten to app/webroot
Any other url needs to be transparently be mapped to the site/ subfolder.
I've tried several things attempting to just redirect everything to a single subfolder but can't even get that working, I have an eternal redirect loop.
<IfModule mod_rewrite.c>
RewriteEngine on
Options +FollowSymlinks
RewriteRule ^\.htaccess$ - [F]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/site/
RewriteRule ^(.*)$ site/$1 [L]
</IfModule>
Try this and see if this works for you.
RewriteEngine on
Options +FollowSymlinks
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/api
RewriteRule ^(.*)$ /app/webroot [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/site
RewriteRule ^(.*)$ /site/$1 [L]

htaccess trailing slash not working

I have the below code in my htaccess. The site works fine (ie: no 500 errors) but it is not adding the trailing slash to my URLs:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteRule ^(.*)$ /index.php?$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://staging.foo.com/$1/ [L,R=301]
</IfModule>
What do I have wrong here?
You need to swap the order of your rules
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://staging.foo.com/$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteRule ^(.*)$ /index.php?$1 [NC,L]
</IfModule>
The reason why it wasn't working was because the routing to /index.php rule was being applied first, then the rewrite engine loops, and then the 2nd rule (the redirect rule) won't get applied because the URI has been rewritten to /index.php and that causes the %{REQUEST_FILENAME} !-f to fail, since index.php exists, thus, the redirect never happens.

Add directory in the middle of a URL with hataccess

My current .htacces file looks like this:
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
What I am trying to accomplish (as well as the above) is to change a url such as http://domain.com/pages/pagename to something like http://domain.com/index.php/pages/view/pagename.
Keeping in mind that I still require urls without the /page/ part such as http://domain.com/search to go to http://domain.com/index.php/search. I am working with CodeIgniter.
What I have come up with so far is:
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
RewriteCond %{REQUEST_URI} ^/pages/(.*)
RewriteRule ^(.*)$ ./index.php/pages/index/$1 [L,QSA]
But it isn't working.
Try this one:
RewriteEngine on
# do not do anything for already existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
# /pages/ specific rule
RewriteRule ^pages/(.*)$ index.php/pages/index/$1 [L]
# everything else
RewriteRule ^(.*)$ index.php/$1 [L]
The above assumes that http://domain.com/pages/ is not a real folder.
RewriteRule ^pages/(.*) /index.php/pages/index/$1