I want use below link and load from category.php but it does not work
http://test.com/?search=x
.htaccess:
RewriteRule ^search$ category.php?search=$1 [QSA,NC]
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/test\.com$
RewriteCond %{QUERY_STRING} ^search=(.*)$
RewriteRule ^(.*)$ http://category.php?search=%1 [R=302,L]
The reason you can't get away with a single RewriteRule is that query parameters cannot be directly rewritten. Instead, the above code captures the query string using RewriteCond %{QUERY_STRING}, which is then made available as %1 in the RewriteRule which follows. The RewriteCond %{REQUEST_URI} ensures that the code will only evaluate for the test.com domain.
Try this,
RewriteEngine On
RewriteCond %{QUERY_STRING} ^search=(.*)$
RewriteRule ^(.*)$ category.php?search=%1 [QSA,NC,L]
You can use this simple rule:
RewriteCond %{QUERY_STRING} ^search= [NC]
RewriteRule ^/?$ category.php [L]
Pattern ^/?$ ensures that we match only landing page, nothing else.
QUERY_STRING is automatically appended to target URI so there is no need to capture and append in target URI.
Related
I have this code in my .htacces file:
RewriteCond %{QUERY_STRING} \\?s=([^&]+) [NC]
RewriteRule ^$ job\?search_keywords=%1 [NC,R,L]
And it works well for: example.com/?s=blabla
Now I would like to add that if the url contains /en/:
https://example.com/en/?s=blabla
change for https://example.com/en/work/?search_keywords=blabla
Somone could help me with it? Because my trying gives me ERROR 500.
You just need another rule to handle new URI pattern:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^s=([^&]+) [NC]
RewriteRule ^$ /job?search_keywords=%1 [R=301,L]
RewriteCond %{QUERY_STRING} ^s=([^&]+) [NC]
RewriteRule ^en/?$ /en/work/?search_keywords=%1 [R=301,L]
Note that \\? in your RewriteCond is redundant as QUERY_STRING variable doesn't contain ?
I want to remove a query string but only for the 6 cases below and never for any subfolders / other url's.
http://www.example.com/?___store=de&___from_store=de
http://www.example.com/?___store=de&___from_store=en
http://www.example.com/en/?___store=de&___from_store=en
http://www.example.com/en/?___store=de&___from_store=en
http://www.example.com/en/?___from_store=en
https//www.example.com/en/?___from_store=de
This is what I already got. It's working, but removes the query string from all url's, what I don't want.
RewriteCond %{QUERY_STRING} /?___store=de&___from_store=de
RewriteRule ^(.*)$ /$1? [L,R=301]
RewriteCond %{QUERY_STRING} /?___from_store=de
RewriteRule ^(.*)$ /$1? [L,R=301]
RewriteCond %{QUERY_STRING} /?___store=de&___from_store=en
RewriteRule ^(.*)$ /$1? [L,R=301]
RewriteCond %{QUERY_STRING} /?___from_store=en
RewriteRule ^(.*)$ /$1? [L,R=301]
I need this specific rewrite rule for a Magento shop. The store view code of the default store is removed by this extension: Knectar/Magento-Store-Codes, see also this: Magento remove store code for default store view.
As a result, the ___store query parameter is added to the url, but only on root.
My solution above does not work, because when it comes to products / categories the language switch is not working correctly anymore (and there is no need to remove the string, because it is stripped out by Magento itself).
Try the following:
RewriteCond %{QUERY_STRING} ^___store=de&___from_store=(en|de)$ [NC]
RewriteRule ^(en/?)?$ $1? [R=301,L,NC]
RewriteCond %{QUERY_STRING} ^___from_store=(en|de)$ [NC]
RewriteRule ^en/?$ $0? [R=301,L,NC]
I'm having an issue with a rather old siye. I have some generic URL's with a query string, that i want to 301 redirect, but I don't want to blanket re-direct the urls. I want to choose where each query string is being redirected as there are alot of different categories within the site. For example:
I want to change:
index.php?_a=viewCat&catId=199
to:
/garden-furniture/patio-furniture/garden-benches-garden-seats/cat_199.html
But ill want to change another catid to another URL of my choosing, completely different structure. The problem I'm having is with the code i've got, if I don't have a ? at the end of the destination url, it works, but appends the query string to the end, if I put it on the end, it doesn't redirect at all.
Code I'm using:
RewriteCond %{QUERY_STRING} ^_a=viewCat&catId=199
RewriteRule ^index\.php$ /garden-furniture/patio-furniture/garden-benches-garden-seats/cat_199.html? [L,R=301]
Any help would be appreciated!
EDIT: The rest of my htaccess
RewriteEngine On
RewriteRule ^conservatory/(.*)$ /conservatory-furniture/$1 [R=301,L]
RewriteRule ^dining-room/(.*)$ /dining-room-furniture/$1 [R=301,L]
RewriteRule ^garden/(.*)$ /garden-furniture/patio-furniture/$1 [R=301,L]
RewriteBase /
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule cat_([0-9]+)(\.[a-z]{3,4})?(.*)$ index.php?_a=viewCat&catId=$1&%1 [NC]
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule prod_([0-9]+)(\.[a-z]{3,4})?$ index.php?_a=viewProd&productId=$1&%1 [NC]
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule info_([0-9]+)(\.[a-z]{3,4})?$ index.php?_a=viewDoc&docId=$1&%1 [NC]
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule tell_([0-9]+)(\.[a-z]{3,4})?$ index.php?_a=tellafriend&productId=$1&%1 [NC]
RewriteCond %{QUERY_STRING} (.*)$
RewriteRule _saleItems(\.[a-z]+)?(\?.*)?$ index.php?_a=viewCat&catId=saleItems&%1 [NC,L]
If these are complete URLs, you could anchor the pattern at the start of the string
RewriteRule ^cat_([0-9]+)(\.[a-z]{3,4})?(.*)$ index.php?_a=viewCat&catId=$1&%1 [NC]
this would prevent an URL, which has cat_ inside being rewritten to index.php?....
And since you don't use the trailing optional part, you could eliminate this too
RewriteRule ^cat_([0-9]+) index.php?_a=viewCat&catId=$1&%1 [NC]
Another point is the RewriteCond with query string. If the query string is optional, you could remove the RewriteCond and modify the RewriteRules to
RewriteRule ^cat_([0-9]+) index.php?_a=viewCat&catId=$1 [QSA,NC]
So, all these would become
RewriteRule ^cat_([0-9]+) index.php?_a=viewCat&catId=$1 [QSA,NC]
RewriteRule ^prod_([0-9]+) index.php?_a=viewProd&productId=$1 [QSA,NC]
RewriteRule ^info_([0-9]+) index.php?_a=viewDoc&docId=$1 [QSA,NC]
RewriteRule ^tell_([0-9]+) index.php?_a=tellafriend&productId=$1 [QSA,NC]
RewriteRule ^_saleItems index.php?_a=viewCat&catId=saleItems [QSA,NC,L]
I need to add style=2 to all urls within a directory ONLY if the url does NOT already contain style=2.
Within the directory root, I have an .htaccess with the following:
RewriteCond %{QUERY_STRING} !(?style=|&style=)
RewriteRule ^(.*)$ %1&style=2? [R=301,QSA]
I know it's wrong. Can anyone help?
Change your code to this to make it work:
RewriteCond %{QUERY_STRING} !(^|&)style=2(&|$) [NC]
RewriteRule ^ %{REQUEST_URI}?style=2 [R=301,L,QSA]
Try this :
RewriteCond %{QUERY_STRING} !^(.*&)?style=
RewriteRule ^(.*)$ /$1?style=2 [L,QSA,R=301]
I'm trying to use mod_rewrite to redirect URLs from a URL from an old host to a new one that uses a different URL format. The new host zero-pads the ID to six digits as follows:
Old URL:
http://www.example.com/script.cgi?page_id=123
Needs to redirect to:
http://archive.example.com/000123/
This is what I have so far:
RewriteCond %{QUERY_STRING} ^page_id=([0-9]+)$
RewriteRule ^script\.cgi http://archive.example.com/%1/? [R=301,L]
This is redirects to:
http://archive.example.com/123/
Is there any way to achieve the zero-padding with mod_rewrite or will I just have to write a handler script to redirect to the proper URL?
Here are two more elegant solutions:
Using N flag to add one zero at a time until we have six digits:
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)page_id=([0-9]{1,5})(&.*)?$
RewriteRule ^script\.cgi$ $0?page_id=0%3 [N]
RewriteCond %{QUERY_STRING} ^([^&]*&)*page_id=([0-9]{6})(&.*)?$
RewriteRule ^script\.cgi$ http://archive.example.com/%2/? [R=301,L]
Using an additional RewriteCond to prepend five zeros and then get just the last six digits:
RewriteCond %{QUERY_STRING} ^(([^&]*&)*)page_id=([0-9]+)(&.*)?$
RewriteCond 00000%3 [0-9]{6}$
RewriteRule ^script\.cgi$ http://archive.example.com/%0/? [R=301,L]
The complex pattern applied on QUERY_STRING is just to consider additional URL parameters. And I prefer the second solution as it is just one rule and more comprehensive.
Not an elegant solution, but you can set up six different rules:
RewriteCond %{QUERY_STRING} ^page_id=([0-9]{1})$
RewriteRule ^script\.cgi http://archive.example.com/00000%1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^page_id=([0-9]{2})$
RewriteRule ^script\.cgi http://archive.example.com/0000%1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^page_id=([0-9]{3})$
RewriteRule ^script\.cgi http://archive.example.com/000%1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^page_id=([0-9]{4})$
RewriteRule ^script\.cgi http://archive.example.com/00%1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^page_id=([0-9]{5})$
RewriteRule ^script\.cgi http://archive.example.com/0%1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^page_id=([0-9]+)$
RewriteRule ^script\.cgi http://archive.example.com/%1/? [R=301,L]
The other option is to use a RewriteMap and have a script generate the URL to redirect to.
RewriteMap zeropad prg:/usr/local/zeropad.pl
RewriteCond %{QUERY_STRING} ^page_id=([0-9]+)$
RewriteRule ^script\.cgi ${zeropad:%1} [R=301,L]
And /usr/local/zeropad.pl is an executable script containing something like
#!/usr/bin/perl
$| = 1; #We don't want buffering
while (<STDIN>) {
printf("http://archive.example.com/%06d/?\n",$_);
}
You can use whatever executable you like instead of a Perl one. The idea is to have it print out (with a trailing newline) the URL to redirect to. It has to work in a loop reading STDIN, as Apache will launch the script once and be calling the same instance repeatedly. It cannot buffer its output, because Apache will wait for its output (until a newline) before completing the rewrite.
Ok, I realized I could achieve the results I wanted with the following:
RewriteCond %{QUERY_STRING} ^page_id=([0-9]{1})$
RewriteRule ^script\.cgi http://archive.example.com/00000%1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^page_id=([0-9]{2})$
RewriteRule ^script\.cgi http://archive.example.com/0000%1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^page_id=([0-9]{3})$
RewriteRule ^script\.cgi http://archive.example.com/0000%1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^page_id=([0-9]{4})$
RewriteRule ^script\.cgi http://archive.example.com/00%1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^page_id=([0-9]{5})$
RewriteRule ^script\.cgi http://archive.example.com/0%1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^page_id=([0-9]{6})$
RewriteRule ^script\.cgi http://archive.example.com/%1/? [R=301,L]
Is there any other way to do this without the multiple rules though?