Replace lowercase word in .htaccess - .htaccess

The pages on my site don't work if you access them by a url which is all lowercase
e.g.
mysite.com/product-category/mens doesn't show correctly
but
mysite.com/product-category/mens does show correctly.
I want to try the .htacess route first of all, but I can't get this to work. What i've tried is the following:
Options +SymLinksIfOwnerMatch +FollowSymLinks -MultiViews
RewriteRule ^/product-category/mens/(.*)$ ^/product-category/Mens/$1 [L]
and
RewriteCond %{QUERY_STRING} ^mens [NC]
RewriteRule ^ %{REQUEST_URI}/Mens [R,L]
But i've very new to .htaccess files and after almost two hours of searching i've decided to ask for help.
Thanks in advance
*********ANSWER***********
Thanks Jon Lin for the solution:
RewriteRule ^product-category/mens/(.*)$ product-category/Mens/$1 [L,R]
RewriteCond %{QUERY_STRING} ^mens(.*)$
RewriteRUle ^ %{REQUEST_URI}?Mens%1 [L,R]

Your first rule is close, you need to remove the leading slash:
Options +SymLinksIfOwnerMatch +FollowSymLinks -MultiViews
RewriteRule ^product-category/mens/(.*)$ ^/product-category/Mens/$1 [L,R]
and for the query string rule you need:
RewriteCond %{QUERY_STRING} ^mens(.*)$
RewriteRUle ^ %{REQUEST_URI}?Mens%1 [L,R]

Related

.htaccess - Rewrite query string and redirect to directory

I have some old URL's that I want to fix because of a forum migration.
The old URL's look like:
http://www.example.com/forum/topic.asp?TOPIC_ID=666
I want to redirect them to:
http://www.example.com/forum/missions/666
My approach is this, but I'm scratching my head, because it doesn't work at all:
RewriteCond %{QUERY_STRING} ^TOPIC_ID=(.*)$ [NC]
RewriteRule ^/forum$ /forum/missions/%1 [NC,L,R=301]
Assuming there is no .htaccess in `/forum/, you can use this first rule in your root .htaccess:
RewriteCond %{QUERY_STRING} ^TOPIC_ID=([^&]+) [NC]
RewriteRule ^forum/topic\.asp$ /forum/missions/%1? [NC,L,R=302]
If there is a .htaccess in /forum/, then you can use this first rule in your /forum/.htaccess:
RewriteCond %{QUERY_STRING} ^TOPIC_ID=([^&]+) [NC]
RewriteRule ^topic\.asp$ /forum/missions/%1? [NC,L,R=302]
I'd suggest this, but cannot really try from here :)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^forum/topic.asp\?TOPIC_ID=([0-9]+)$ forum/missions/$1 [L]
</IfModule>

How can I add conditional url variable using htaccess?

I use following code:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{QUERY_STRING} (^|&)post_type=product(&|$)
RewriteCond %{QUERY_STRING} !(^|&)product_cat=xyz
RewriteRule ^(.*)$ http://www.example.com/?%{QUERY_STRING}&product_cat=xyz
but it doesn't works.
I need to add variable "product_cat=xyz" where query_string contains "post_type=product" and it doesn't just contains "product_cat=xyz".
Examples:
If url is "http://www.example.com/?post_type=product&product_cat=xyz" then htacces leaves it unchanged.
If url is "http://www.example.com/?post_type=product" then htacces must add "&product_cat=xyz"
:-) Can you help me, please?
Thanks in advance.
You can use:
RewriteEngine on
RewriteCond %{QUERY_STRING} (^|&)post_type=product(&|$)
RewriteCond %{QUERY_STRING} !(^|&)product_cat=xyz(&|$)
RewriteRule ^ http://www.example.com/?product_cat=xyz [QSA,L]

Url changed but page remains same

I want to change my url
http://www.abc.com/search_result.php?id=110
to
http://www.abc.com/110
Here is the code which i am using.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+search_result\.php\?id=([^\s]+) [NC]
RewriteRule ^ http://abc.com/%1? [R=301,L]
But the problem is, url changed to http://www.abc.com/110 , but page remain same.
Please anybody help !
One thing more i want to ask . Suppose i want to add more parameter in original url:
Say,
http://www.abc.com/search_result.php?id=110&name=amit
then what i should do to get the result.
http://www.abc.com/i-am-amit
Thanks !
You need an internal rewrite rule also for showing actual content from search_result.php"
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+search_result\.php\?id=([^&\s]+)\s [NC]
RewriteRule ^ http://abc.com/%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ search_result.php?id=$1 [NC,L,QSA]
Also for http://www.abc.com/search_result.php?id=110&name=amit how do you want pretty URL to be? Keep in mind that you will need both id & name in pretty URL such as:
http://www.abc.com/110/amit
Is that how you want?

.htaccess not matching

htaccess not matching
RewriteCond %{REQUEST_URI} ^/catalog/products_in_scene.php?(.*)$
RewriteRule ^(.+) "/services/hpv/index.php?%1"
RewriteCond %{REQUEST_URI} ^/shop/derivation_tree.php?(.*)$
RewriteRule ^(.+) "/services/dt/index.php?%1"
The top one matches fine with all the GET variables, the second one matches and sends me to the right page but never sends it the GET variables; Why?
Remove the quote from your RewriteRule.
If it doesn't work, that mean there is something wrong in you php GET variable.
You'd do better to use the [QSA] flag:
RewriteRule ^/shop/derivation_tree.php /services/dt/index.php [QSA]
Change your code to this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^catalog/products_in_scene\.php$ services/hpv/index.php [R,L,NC]
RewriteRule ^shop/derivation_tree\.php$ services/dt/index.php [R,L,NC]

How to use .htaccess to redirect a url which has a # character

I've been looking at this for a couple of days, even had the IT guys at work try and figure it out but we didn't get very far.
I have a url my.website.com/index.php#!lightBox[gallery]/0/ which I want to redirect to just my.website.com. I can do it fine with just the index.php, but it's the extra parts that aren't working. I've tried:
RewriteCond %{HTTP_HOST} ^my.website.com$ [NC]
RewriteRule (.*) http://my.website.com/index.php#!lightBox[gallery]/0/? [R=301,L]
and cpanel generated the following redirect:
RewriteCond %{HTTP_HOST} ^my.website.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.my.website.com$
RewriteRule ^/?$ "http\:\/\/my\.website\.com\/\#\!lightBox\[gallery\]\/0\/" [R=301,L]
Both output my.website.com/%23!lightBox[gallery]/0/ as the url - as you can see the hash isn't being processed.
If anyone has any ideas I'd love to hear!
Thanks :)
Have your rules like this:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?my\.website\.com$ [NC]
RewriteRule ^/?$ http://my.website.com/index.php#!lightBox[gallery]/0/ [R=301,L,NE]
You do need to escape dot . in RewriteCond but don't need to escape any special characters on RHS. Importantly you need to have flag NE to not to escape the resulting URI.

Resources