I am trying to create a htaccess file for my site.
I would like it so that when a user enters http://sitename.co.uk/category/productname/?pid=productID they are shown the page http://sitename.co.uk/product.php?id=productID
So the category and the productname are essentially wildcards and can be anything as the page is powered by the productID.
I have tried writing the following but have had no luck with it...
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/(?pid=.*)$ product.php?id=$1 [L,NC]
You should can try like this
RewriteCond %{QUERY_STRING} pid=([^&]+)
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+) /product.php?id=%1
%1 is back-reference for RewriteCond. $1 is back-reference for RewriteRule.
You will need to capture value of pid parameter from a RewriteCond as RewriteRule doesn't match query:
RewriteCond %{QUERY_STRING} ^pid=([^&]+) [NC]
RewriteRule ^([\w-]+)/([\w-]+)/?$ product.php?id=51 [L,NC,QSA]
Related
I need to redirect a URL with parameters to a custom URL in htaccess.
example.com/folder?misc-params-that-can-change
should redirect to
my.customlink.com
I don't know how to write the wildcard for the parameters that start with the question mark.
This is where I'm at now:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/folder [NC]
RwriteRule ^folder?(.*)$ http://www.google.com/$1 [R=301,L]
You may use this rule:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/folder/?$ [NC]
RewriteCond %{QUERY_STRING} .
RewriteRule ^ http://www.google.com/? [R=301,NC,L]
You cannot match query string in the pattern of RewriteRule, need to use %{QUERY_STRING} variable in RewriteCond construct for this purpose.
? after target URL is used to discard previous query string.
This silent redirect in htaccess:
RewriteCond %{QUERY_STRING} ^pic_id=(\d+)$ [NC]
RewriteRule ^album_showpage\.php$ /gallery/image_page.php?image_id=%1 [L]
redirect a url that looks like:
example.com/album_showpage.php?pic_id=1906
to:
example.com/gallery/image_page.php?image_id=1906
that works great. But when the URL has a parameter like:
album_showpage.php?pic_id=1906&mode=prev
or
album_showpage.php?pic_id=1906&mode=next
the redirect wont work.
Question: How to cut of any parameter after pic_id=1906
thank you
You need match against the rest of the query string.
RewriteCond %{QUERY_STRING} ^pic_id=(\d+)(&.*)?$ [NC]
RewriteRule ^album_showpage\.php$ /gallery/image_page.php?image_id=%1%2 [L]
If you don't want the mode=prev stuff to be included in the rule's target, then you can simply remove the $ instead of attempting to match against it:
RewriteCond %{QUERY_STRING} ^pic_id=(\d+) [NC]
RewriteRule ^album_showpage\.php$ /gallery/image_page.php?image_id=%1 [L]
Hey guys I'm having a bit of trouble getting my htaccess to redirect properly and was hoping for some help.
I'm expecting DEV-domain.com?CampID=AB12345 to redirect to
http://DEV-www.domain.com/landing/external-marketing/direct-mail/AB?CampId=AB12345
RewriteCond %{HTTP_HOST} ^DEV-(www\.)?domain\.com [NC]
RewriteCond %{QUERY_STRING} ^CampID=
RewriteRule (\w{2})(\w{5})$ http://DEV-www\.domain\.com/landing/external-marketing/direct-mail/$1?CampId=$1$2 [R=301,L]
Unfortunetly I can't get it working for some reason?
Because the RewriteRule matching is meant for the url path, not query strings. Try this:
RewriteCond %{HTTP_HOST} ^DEV-(www\.)?domain\.com [NC]
RewriteCond %{QUERY_STRING} ^CampID=(\w{2})(\w{5})
RewriteRule .* http://DEV-www.domain.com/landing/external-marketing/direct-mail/%1?CampId=%1%2 [R=301,L]
also you don't need to escape dots . in the target url, only in matching patterns. And be aware that if you decide to make your target url CampID instead of CampId, you need to put in another condition:
RewriteCond %{REQUEST_URI} !^/landing/external-marketing/direct-mail/
to avoid an infinite redirect as a target with CampID would match your RewriteCond rule...
i have some links that i need to redirect, like:
/pass-help.php?action=1&email=test#test.com to /about/help
or
/contests/contest-a?testa=1&testb=1 /user/index/contest
i need a way to find the action or the email in the links above because they can be anything.
maybe something like /pass-help.php?action=(\?.+)&email=(\?.+) to /about/help
but im not sure how to write the query string.
any ideas?
if so, please explain a bit so i can understand what is happening.
thanks
If the query strings don't matter, you can just ignore them:
via mod_alias:
Redirect 301 /pass-help.php /about/help?
Redirect 301 /contests/contest-a /user/index/contest?
via mod_rewrite:
RewriteRule ^/?pass-help\.php$ /about/help? [L,R=301]
RewriteRule ^/?contests/contest-a /user/index/contest? [L,R=301]
Note that in the mod_alias case, there will be a stary ? at the end. That's needed in order to get rid of the query string.
If the query strings need to have some param names, but you don't care about the values:
via mod_rewrite:
RewriteCond %{QUERY_STRING} (^|&)action=
RewriteCond %{QUERY)STRING{ (^|&)email=
RewriteRule ^/?pass-help\.php$ /about/help? [L,R=301]
RewriteCond %{QUERY_STRING} (^|&)testa=
RewriteCond %{QUERY_STRING} (^|&)testb=
RewriteRule ^/?contests/contest-a /user/index/contest? [L,R=301]
If the query strings need to have some param names with EXACTLY some value:
RewriteCond %{QUERY_STRING} (^|&)action=1
RewriteCond %{QUERY)STRING{ (^|&)email=test#test.com
RewriteRule ^/?pass-help\.php$ /about/help? [L,R=301]
RewriteCond %{QUERY_STRING} (^|&)testa=1
RewriteCond %{QUERY_STRING} (^|&)testb=1
RewriteRule ^/?contests/contest-a /user/index/contest? [L,R=301]
I want to get rid of some query string on my whole website (explicitly facebook shared/like query which usually begin with fb_action).
I thought about using .htaccess to do that.
I want this:
Link 1)
http://example.com/documents/de_desmarais_en_sirois?fb_action_ids=10151430962018298&fb_action_types=og.likes&fb_source=timeline_og&action_object_map={%2210151430962018298%22%3A157643874359578}&action_type_map={%2210151430962018298%22%3A%22og.likes%22}&action_ref_map=[]
to look like:
Link 2)
http://example.com/documents/de_desmarais_en_sirois
In my .htaccess, I added:
RewriteCond %{QUERY_STRING} fb_action
RewriteRule ^(.*) /$1? [R=301,L]
But, when I go on the original link, I am directed to my root folder example.com/pages.php, but I want to keep the first part of the URL which is example.com/documents/de_desmarais_en_sirois. I don't want to go to my root.
What should I modify/do?
I assume you've added those rules to the htaccess file in the /documents/ directory?
You'll need to remove the leading slash in your rule's target and add a rewrite base:
RewriteBase /documents/
RewriteCond %{QUERY_STRING} fb_action
RewriteRule ^(.*) $1? [R=301,L]
The query string match could be improved a bit though:
RewriteCond %{QUERY_STRING} (.*)(^|&)fb_[^&]+(.*)$
RewriteRule ^(.*) $1?%1%2 [R=301,L]
RewriteCond %{QUERY_STRING} (.*)(^|&)action_[^&]+(.*)$
RewriteRule ^(.*) $1?%1%2 [R=301,L]
To remove only the facebook query string parameters, if you have other parameters that you want to preserve.