htaccess redirection conflict - .htaccess

I am writing code in htaccess to add trailing slashes in url, also applying redirection.
My code is-
#Add slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://example.com/$1/ [L,R=301]
Redirect 301 /social-media-consultant http://example.com/seo-consultant/
Redirect 301 /uiux-developers http://example.com/graphics-designer/
Now, problem is that when I write code for adding slashes then redirection stops working.

Change order of your rules
Use only mod_rewrite based rules.
Have it this way:
RewriteEngine On
RewriteRule ^social-media-consultant$ http://example.com/seo-consultant/ [L,NC,NE,R=301]
RewriteRule ^uiux-developers$ http://example.com/graphics-designer/ [L,NC,NE,R=301]
#Add slashes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^ http://example.com/%{REQUEST_URI}/ [L,R=301,NE]
Make sure to clear your browser cache before testing this change.

Related

htaccess /index.php?u=xx&p=yy to /xx/yy/ URL rewrite

I would like to know how to URL change /index.php?u=xx&p=yy to /xx/yy/.
index.php will be removed and u and p values become with a slash mark.
htaccess code:
RewriteCond %{THE_REQUEST} /index\.php[\s?/] [NC]
RewriteRule ^(.*?)index\.php(/.*)?/?$ /$1$2 [L,R=301,NC,NE]
I have used the above code but it only removes index.php. Others are remaining the same.
With your shown samples/attempts, please try following htaccess rules. Please make sure to clear your browser cache before testing your URLs.
Also please make sure that your htaccess rules file and index.php files are in same directory.
RewriteEngine ON
##External redirect rules as follows:
RewriteCond %{THE_REQUEST} \s/p/index\.php\?u=([^&]*)&p=(\S+)\s [NC]
RewriteRule ^ /%1/%2? [R=301,L]
##Internal rewrite rules as follows:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/(.*)/?$ index.php?u=$1&p=$2 [QSA,L]

Removing trailing slash from htaccess redirection

I want to make a redirection using htaccess but I run into an issue with trailing slash.
I want to redirect from https://test.com/transfer or https://test.com/transfer/
to https://test2.com/destination
Below is my htaccess rule but it seems to be only working for https://test.com/transfer but NOT https://test.com/transfer/
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/transfer$ [NC]
RewriteRule ^ https://somesite.com/destination/$1 [R,L]
What is wrong with my rule?
Try:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/transfer/?$ [NC]
RewriteRule ^ https://somesite.com/destination/$1 [R,L]
or
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/transfer/?$ https://somesite.com/destination/ [NC,R,L]
Make sure you use RewriteEngine On only once.
If you want to redirect everything inside transfer: if transfer is a dir btw, use:
RewriteEngine on
RewriteRule ^/transfer/?(.+)?$ https://somesite.com/destination/$1 [R, L]

How to redirect rewritten url

URL is rewritten using following rule.
RewriteEngine on
RewriteRule ^([a-zA-Z]+)/$ category?id=$1
to change
http://localhost/newsite/category?id=home
to following structure
http://localhost/newsite/home/
Now I tried to redirect, newsite/category?id=home to newsite/home/, to make clean URL using redirect rule, such as 301, redirect, but it doesn't work.
You can use this set of rules in newsite/.htaccess:
RewriteEngine on
RewriteBase /newsite/
RewriteCond %{THE_REQUEST} /category(?:\.php)?\?id=([^\s&]+) [NC]
RewriteRule ^ %1? [R=301,L]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/$ category.php?id=$1 [L,QSA]
You missed the .php in the rewrite statement
RewriteRule ^([a-zA-Z]+)/$ category?id=$1
change to
RewriteRule ^([a-zA-Z]+)/$ category.php?id=$1

Remove trailing slash using .htaccess for a particular page

I've modified my .htaccess file to force trailing slashes on all the pages but I'm wondering how to remove the trailing slash in a particular url page? Need help. For example:
abc.com/test successfully redirects to abc.com/test/
But I want to remove that force trailing slash in a particular url of the site,
abc.com/demo/ should redirect to abc.com/demo
Here what I have done so far:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/demo/
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]
Update:
To remove trailing slashes for multiple urls what would be the code, eg abc.com/demo2/ abc.com/demo3/ abc.com/demo4/ etc.. Any suggestions?
Try :
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/demo/ [NC]
RewriteRule ^demo/$ /demo [L,R]
RewriteCond %{REQUEST_URI} !^/demo [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]

.htaccess 301 Redirect non slash domains and slash domain to .html

I am trying to add some code to my .htaccess to redirect slash and non slash urls to the .html all url's apart from my homepage.
For example
www.mydomain.com/cat/ and www.mydomain.com/cat
should redirect to www.mydomain.com/cat.html
I have managed to add the following to my .htaccess which redirects www.mydomain.com/cat to the right place www.mydomain.com/cat.html but need some help on how to make slash version redirect to the .html page
RewriteCond %{REQUEST_URI} !\.[^./]+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://mydomain.com/$1.html [R=301,L]
My whole .htaccess looks like this, if anyone has any suggestions on how it should look in light of the above it would be greatly appreciated.
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^xxx.xxx.xxx.xx [nc,or]
RewriteCond %{HTTP_HOST} ^mydomain.com [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
RewriteCond %{REQUEST_URI} !\.[^./]+$
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://mydomain.com/$1.html [R=301,L]
DirectoryIndex index.html index.php
<IfModule mod_rewrite.c>
RewriteEngine on
# Pleas note that RewriteBase setting is obsolete use it only in case you experience some problems with SEO addon.
# Some hostings require RewriteBase to be uncommented
# Example:
# Your store url is http://www.yourcompany.com/store/cart
# So "RewriteBase" should be:
# RewriteBase /store/cart
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !\.(png|gif|ico|swf|jpe?g|js|css)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php?sef_rewrite=1 [L,QSA]
</IfModule>
SOLVED:
I just added:
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^(.+)/$ /$1 [R=301,L]
The separate rules seems to be working for you, but I think you can simplify it to one rule, with an optional slash. Your rule redirects the slash to no-slash, which then redirects again to the .html. With one rule, you'd only have one redirect.
This has the standard RewriteCond that check if it's not a file or a folder, so it doesn't keep redirecting .html if it's already one. Then, the \? in the ReweriteRule is an optional slash.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ http://mydomain.com/$1.html [R=301,L]
If this is all in your domain, you can omit it from the result:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ /$1.html [R=301,L]
Also, note this will catch and work with subfolders, whether or not you mean it to. e.g.,
www.mydomain.com/animals/cat/ will redirect to www.mydomain.com/animals/cat.html

Resources