I'm trying to remove part of a URL with my .htaccess file. I have some previous rewrites that are working.
The url is https://example.com/index.php?route=product/product&product_id=5062&manufacturer_id=57
I have the index.php? removed with this rule, and it's working
RewriteEngine On
RewriteBase "/"
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} (route.*)
RewriteRule "(.*)index.php" $1%1? [R=301,L]
Now there are some urls that have manufacturer_id=1-4 numbers, and I'm trying to remove those. I'm getting an internal redirect now.
The rule is
RewriteCond %{QUERY_STRING} (&manufacturer_id=[0-9]{1,4})
How can I remove it without it tripping over my other rules?
Thanks
Place this rule at top of your .htacces to remove query parameter:
RewriteCond %{THE_REQUEST} \?(.*&)?manufacturer_id=[^&]*&?(\S*)\s [NC]
RewriteRule ^ %{REQUEST_URI}?%1%2 [R=301,NE,L]
Related
I am trying to rewrite a URL so that it is SEO friendly and excludes the product ID. Currently I am using this in my .htaccess file:
RewriteRule ^shop/product/([^/\.]+)/([^/\.]+)$ products.php?id=$2&name=$1 [NC,L]
For example rewrites it to:
shop/product/product-name/12
Is there a way that I can rewrite this URL so it removes or excludes the product ID from the end?
Could you please try following, written and tested with shown samples. Please make sure you clear your browser cache before testing your URLs.
First set of Rules is considering that your product.php is present in root, in same path as .htaccess is present.
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^shop/product/([^/]*)/([^/]*)/?$ /products.php?id=$2&name=$1 [NE,R=301,NC]
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{THE_REQUEST} \s/products\.php\?id=[^&]*&name=[^\s]*\s [NC]
RewriteRule ^(.*)$ shop/product [R=301,QSD,NE,L]
OR Please use either above OR following rules at a time only in your .htaccess.
RewriteEngine ON
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^shop/product/([^/]*)/([^/]*)/?$ products.php?id=$2&name=$1 [NE,R=301,NC]
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{THE_REQUEST} products\.php\?id=[^&]*&name=[^\s]*\s [NC]
RewriteRule ^(.*)$ shop/product [R=301,QSD,NE,L]
I try to solve folling problem with .htaccess.
I use it to rewrite everything to "https://" and put "www" in front of every url.
Now I want to use a SSL-certificate. To validate it, I put a html-file in a certain folder. I do not want this to be redirected to "www.". How can I create an exception only for this one file?
Thank you very much for helping me with this maybe kind of stupid question.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
You did not provide an example of URL that should not be redirected to https. But here is an example. This is a simplified version of your .htaccess file.
I have combined the https and www redirect in one directive.
Assume your domain name is example.com.
There is a negative condition (note the exclamation mark). If the request URI does not begin with index.html then the rule fails and is not enforced.
So http://example.com/index.html will not be converted to https, but there is one important caveat: in this case the www. will not be added either. I understand this what you want, then we can have a simplified set or rules.
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{REQUEST_URI} !^/index.html
RewriteRule (.*) https://www.example.com/$1 [L,R=301]
I want to redirect pages like:
/category-name/post-name.html?id=1234
To:
/category-name/1234-post-name.html
How can do this using htaccess?
What I have tried:
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^([^/]+)/([^.]+)\.html$ /$1/%1-$2\.html [L,R=301]
But it is a continuous redirect.
You can use these rules in your site root .htaccess:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+([\w-]+)/([\w-]+)\?id=([^\s&]+) [NC]
RewriteRule ^ /%1/%3-%2? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^([\w-]+)/([^-]+)-([\w-]+)/?$ $1/$3?id=$2 [L,QSA]
Try this
Make sure the url is root url
Example:-
www.foo.com/category-name/post-name.html
It will only work if the project url is same as that of the above.
www.foo.com/blog/category-name/post-name.html
This won't work you need to update the RewriteBase url accordingly l.
This is the conditions
RewriteEngine On
RewriteBase /
RewriteCond %(REQUEST_FILENAME) !-f
RewriteCond %(REQUEST_FILENAME) !-d
RewriteRule here
RewriteRule ^category-name/([0-9]+)-post-name$ /category-name/post-name.html?id=$1 [L]
This should work..
I've an old website url
http://www.babycareadvice.com/babycare/general_help/article.php?id=93&0_id=1#591
which i want to redirects to
http://www.babycareadvice.com/article/detail/93
but it redirects to
http://www.babycareadvice.com/article/detail/93#591
how i remove this #591 part in url.
The code i've written in .htaccess is here
RewriteCond %{THE_REQUEST} /babycare/general_help/category\.php\?id=93]+) [NC]
RewriteRule ^/?(http://babycareadvice.com)? /article/detail/93? [R=301]
Try below rule,
RewriteEngine On
RewriteCond %{REQUEST_URI} ^babycare/general_help/article.php [OR]
RewriteCond %{QUERY_STRING} ^id=([\d]+)&0_id=1#591
RewriteRule ^ article/detail/%1 [R=301,L]
From what I know hash will not entertained by apache.
Note: I didn't tried it for now but similar rule worked for me.
I have a question about htaccess.
I have a url like this :
http://www.asd.com/producten/kia?category=car
And I want this to be :
http://www.asd.com/producten/car/kia
Is that possible with htaccess?
I have tried different ways to solve it but so far no luck.
I've tried:
RewriteRule ^producten/([^/]+)/([^/]+) /producten/$2?category=$1 [NC]
AND
RewriteRule /producten/(.*)/(.*) /producten/$2?category=$1 [R]
Thanks in advance,
Mert
Try using the following in your /.htaccess file:
RewriteEngine On
# Step 1: Redirect the old URI to the new one and prevent looping
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} category=([^\s&]+) [NC]
RewriteRule ^(producten)/([^/]+)/? /$1/%1/$2? [R=302,L,NE]
# Step 2: Internally rewrite the new URI to the old one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(producten)/([^/]+)/([^/]+)/?$ /$1/$3?category=$2 [L,QSA]
If you would like to make the redirect permanent, change R=302 to R=301.
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /(producten)/([^?]+)\?category=([^\s&]+) [NC]
RewriteRule ^ /%1/%3/%2? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^(producten)/([^/]+)/([^/]+)/?$ $1/$3?category=$2 [L,QSA,NC]