RewriteRule with {QUERY_STRING} - .htaccess

I need to make a redirect form an old page to a new one withing same webdite. The problem is that the old URL has {QUERY_STRING}.
Old http://hostelcomfort.com.ua/?page_id=12
New http://hostelcomfort.com.ua/o-gostinitse/
So far the best I could come up with was
RewriteCond %{QUERY_STRING} ^page_id=(.*)$
RewriteRule ^/?$ /o-gostinitse/ [R=301,L]
However it keeps adding the query string at the end /o-gostinitse/?page_id=12.
How can I edit the second line?

You need to use:
RewriteCond %{QUERY_STRING} ^page_id=12$
RewriteRule ^/?$ /o-gostinitse/? [R=301,L]
? in the target URL will discard any existing query string.
PS: Starting from Apache 2.4 you can also use QSD (Query String Discard) flag:
RewriteCond %{QUERY_STRING} ^page_id=12$
RewriteRule ^/?$ /o-gostinitse/ [R=301,L,QSD]

Related

Remove parenthesis from htaccess (Helicon Ape)

I'm using Helicon Ape on a Windows Server to create htaccess files.
Originally, part of a larger set of conditions, I had this condition set to return 403 if the url contained (). However it is causing false positives in case of mailchimp tracking codes that end up getting wrapped in ()
RewriteCond %{QUERY_STRING} ^.*(\[|\]|\(|\)|<|>).* [NC,OR]
For example in the below URL
http://domain.com/page/11/page-name?ct=t(Newsletter_Tracking)
As an alternative, I was attempting to remove the parenthesis and redirect to a "cleaned version".
I tried a few different things that I found in SO but none worked.
So far the closest thing that I could get to working is this:
RewriteCond %{REQUEST_URI} [\(\)]+ [OR]
RewriteCond %{QUERY_STRING} [\(\)]+
RewriteRule ^(.*)[\(]+([^\)]*)[\)]+(.*)$ $1$2$3 [R=301,L]
The problem with the above code is that works if the () were in the URL but not the query string. It doesn't redirect and clean the querystring.
So this would work:
http://domain.com/page/11/pag(e-name)
but this wouldn't:
http://domain.com/page/11/page-name?ct=t(Newsletter_Tracking)
Your assistance is appreciated
Thank You.
You can use the following rule :
RewriteCond %{THE_REQUEST} /page/11/page-name\?ct=t\(Newsletter_Tracking\)\sHTTP [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R]
If the querystring is dynamic, try:
RewriteCond %{THE_REQUEST} /page/11/page-name\?ct=.+\(.+\)\sHTTP [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R]
Using #starkeen 's example, I was able to create a working solution.
This code handles the Query String separate from the URL. It cleans the URL but removes the query string.
RewriteCond %{REQUEST_URI} [\(\)]+
RewriteRule ^(.*)[\(]+([^\)]*)[\)]+(.*)$ $1$2$3 [R=301,L]
RewriteCond %{QUERY_STRING} [\(\)]+
RewriteRule ^ %{REQUEST_URI}? [R=301,L]

.htaccess remove GET completely

I have dozens of redirects from an old page e.g. index.php?mode=1,2,3,0 and I want to get rid of all GET Params because the new page is anyways just plain html.
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} mode=17,0,0,0,0$
RewriteRule (.*) /big-mamas-house/ [R=301,L]
I thought removing (.*) would already do the trick but then the rule is not applied anymore according to:
http://htaccess.madewithlove.be/
Your rule can simplified to:
RewriteCond %{QUERY_STRING} ^mode=17,0,0,0,0$
RewriteRule ^index\.php$ /big-mamas-house/? [R=301,L,NC]
? in the end is needed to strip off any previous query string.

Multipart htaccess redirect

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...

Strip query_string

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.

.htaccess mod_rewrite: rewriting querystring to path

How could I use a rewrite to change:
/?tag=foo
To:
/tag/foo
I tried:
RewriteCond %{QUERY_STRING} ^tag=(.+)$
RewriteRule ^(.*)$ http://www.example.com/tag/$1 [L]
But it did not work.
Try the following:
RewriteCond %{QUERY_STRING} ^tag=(.+)$
RewriteRule ^(.*)$ http://www.example.com/tag/%1 [L]
Usually, rewrites are used to achieve the opposite effect. Are you sure you don't really want to do the following?
RewriteRule ^tag/(.+)$ index.php?tag=$1 [L]
To avoid recursion, you should check the request line instead as the query string in %{QUERY_STRING} may already have been changed by another rule:
RewriteCond %{THE_REQUEST} ^GET\ /\?(([^&\s]*&)*)tag=([^&\s]+)&?([^\s]*)
RewriteRule ^(.*)$ /tag/%3?%1%4 [L,R=301]
Then you can rewrite that requests back internally without conflicts:
RewriteRule ^tag/(.*) index.php?tag=$1 [L]
I was trying to convert from a URL like this:
http://java.scandilabs.com/faq?key=Contents_of__gitigno
To a URL like this:
http://scandilabs.com/technology/knowledge/Contents_of__gitigno
Andrew's response above worked for me with the addition of a question mark at the end to discard the original query string:
RewriteCond %{HTTP_HOST} ^java
RewriteCond %{QUERY_STRING} ^key=(.+)$
RewriteRule ^(.*)$ http://scandilabs.com/technology/knowledge/%1? [R=301,L]
Note if you're on apache 2.4 or higher you can probably use the QSD flag instead of the question mark.

Resources