is this query correct for redirecting example.com/?abcd2 to example.com/abcd2.php
RewriteCond %{QUERY_STRING} ^?abcd2$
RewriteRule (.*) http://example.com/abcd2.php [R=301,L]
No there are some minor glitches. Use this rule:
RewriteCond %{QUERY_STRING} ^abcd2$
RewriteRule ^ http://example.com/abcd2.php? [R=301,L]
QUERY_STRING value doesn't start with ? and you need ? in target URI to strip off existing query string.
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]
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]
How do i remove a query string from the end of a url?
To be more specific, this is my rewrite rule:
RewriteRule example-(.*).html$ examples/view-example.php?param1=parameter¶m2=$1&split=-
and I want this to return a 404 or a redirect to www.mydomain.com/example-one.html :
www.mydomain.com/example-one.html?param1=parameter¶m2=one&split=-
This is what i tried, it doesn't work:
RewriteCond %{REQUEST_URI} /examples/view-example\.php
RewriteCond %{QUERY_STRING} param1=parameter¶m2=(.*)&split=-
RewriteRule ^(.*)$ http://mydomain.com/example-%1.html$
I think that RewriteRule ^(.*)$ http://mydomain.com/example-%1.html$ isn't correct..
This should do the trick:
RewriteCond %{QUERY_STRING} ^param1=parameter¶m2=(.*)&split=-
RewriteRule ^/examples/view-example\.php$ http://mydomain.com/example-%1.html [R=301]
Though, I didn't understand what you wanted to do with www.mydomain.com/example-one.html?param1=parameter¶m2=one&split=-
My client wants a query string munged (by changing % to A) on certain pages.
For example, I can remove the query string completely on the desired pages via:
RewriteCond %{QUERY_STRING} !=""
RewriteCond %{REQUEST_URI} ^/SpecialPage(.*)
RewriteRule ^(.*)$ /$1? [R=301,L] #remove query string
Here's what I thought should remove % on the query string and replace with A but it's not:
RewriteCond %{QUERY_STRING} ^(.*)\%(.*)$
RewriteCond %{REQUEST_URI} ^/SpecialPage(.*)
RewriteRule ^(.*)$ /$1?%1A%2 [L]
What am I doing wrong in this? I just can't quite spot it. Thanks for the expert eyes!
You're real close.
The problem here is that you've got a condition and the match of your rule should be together. Your backreference to the previous RewriteCond is broken because it's for the REQUEST_URI and not the QUERY_STRING like you want.
RewriteCond %{REQUEST_URI} ^/SpecialPage(.*)
RewriteRule ^(.*)$ /$1?%1A%2 [L]
Here, the %1 backreference matches the (.*) at the end of the /SpecialPage URI. The backreferences from your query string match gets lost, and that's the ones you really want. You can combine the condition to match the REQUEST_URI with the regular expression pattern in the RewriteRule:
RewriteCond %{QUERY_STRING} ^(.*)\%(.*)$
RewriteRule ^SpecialPage(.*)$ /SpecialPage$1?%1A%2 [L]
Here, the %1 and %2 backreferences correctly reference the query string and the SpecialPage condition in the URI is met by the regex pattern.
Redirect Query String
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.)=(.)$
RewriteRule ^(.)?(.)$ /$1--%0? [R=301,L]
From Url: http://localhost/sholay-slide.jsp?slide=2
To Url: http://localhost/sholay-slide.jsp--slide=2