Remove query string from redirected URL with htaccess - apache

I'm using the following code to redirect traffic to a spesific page (this traffic is coming via google from an old site which used to use my servers ip)
RewriteRule ^viewtopic.php?/?$ http://www.myurl.org.uk/ [L,R=301]
As I understand it this ^viewtopic.php?/?$ should strip away the query string but it isn't working. Any help appreciated.
Example URL
http://www.myurl.org.uk/viewtopic.php?f=3&t=44207&start=2265
Output when redirected
http://www.myurl.org.uk/?f=3&t=44207&start=2265

You were close to the answer... You have the ? on the wrong side. Put it on the redirect side to strip off the query string:
RewriteRule ^viewtopic.php http://www.myurl.org.uk/? [L,R=301]
In a 301 redirect, mod_rewrite will normally append the full query string. But placing a ? at the end of your rewritten URL without a corresponding [QSA] ("querystring append") flag will instruct it instead to use the blank query string you supplied.

Related

301 redirect for old urls with language parameter

I need a hand with some 301 redirects for my apache htaccess file. The old urls contain variables at the end and have structures like the following:
/furniture-248/category/570-shelves.html?lang=en
/all-products/furniture-248/shelves.html?page=2&lang=en
/store/product/asearch.html?path=7_632&lang=en&Itemid=284
The new urls don't contain parameters of this nature and would be simply of the form:
main-cat/subcat/sale.html
I tried a regular 301 redirect in the htaccess file which works for urls without parameters but those urls containing the ?lang=en simply don't work.
This is what I was trying:
Redirect 301 /furniture-248/category/570-shelves.html?lang=en http://www.domain.com/shelves.html
I'd be very grateful for any help and advice.
Many thanks in advance
You can't use the query string as part of a redirect like that. You have two options.
Option 1
Take the "?lang=en" part off and just redirect all instances of that URL, whatever the query string is.
Redirect 301 /furniture-248/category/570-shelves.html http://www.domain.com/shelves.html
This will leave the query string intact, so the new URL will include "?lang=en" if it is present, or any other query string.
But of course, you might need to only redirect it when it has the "?lang=en" part, or leaving the query string intact when redirecting might not be acceptable. In that case, it will need to be...
Option 2
Use mod_rewrite:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^lang=en$
RewriteRule ^furniture-248/category/570-shelves\.html$ http://www.domain.com/shelves.html? [R=301,L]
This does exactly what you asked for, redirecting /furniture-248/category/570-shelves.html?lang=en to http://www.domain.com/shelves.html and only that.
Note that..
The query string is matched separately.
The opening forward slash on the matching part is not used (because the fact you're in a website root level .htaccess file implies that opening slash).
The closing question mark on the redirect URL is important, as it tells the engine to drop the existing query string, which is what you want.
[R=301,L] means do a 301 redirect and don't process any more URL rewriting on this URL.
For the matching part in RewriteRule, the dot before "html" is escaped with "\" because dot has a special meaning in a regex.
Also for the matching parts, in both RewriteCond and RewriteRule, the ^ means the start of the string and the $ means the end of it, so that we are matching exactly that rather than it being possible for it to be part of a longer string.
And finally, if you're adding a number of these, you only need the "RewriteEngine On" part once, at the top. The other two parts are needed for each one.
Please be sure to test all redirects you add with this method as there is more to mod_rewrite than I have mentioned in this simple explanation.

Apache rewrite rule percent symbol - facebook

I am having this issue with my apache and rewrite rules.
My orignal url is something like this:
urbana.com.uy/core.php?m=amp&nw=MTQ2NA==
When I post it in facebook, the URL change (facebook do it) to this:
urbana.com.uy/xcore/?m=nw&nw=MTQ2NA%3D%3D
So, facebook convert the = symbol to %3D
Well, from here no problem.
BUT, I have a rewrite rule in my server that rewrite an URL that doesnt start with www to www.blabla
This are the rules:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}$1 [R=301,L]
The work but with a problem.
When a user clic in a URL from facebook, with the %3D instead of =, my apache change that for a %25, like this
www.urbana.com.uy/xcore/?m=nw&nw=MTQ2NA%253D%253D
and this doesnt work
How can I tell my apache to not change the % symbol to %25 and redirect all the non www no www?
Thanks
I think the only reliable answer to your problem is to not send non-url safe values to as the value of a URL query. I would suggest using some form of url encode on your parameter before you send it to facebook, and then a url decode on the other side. In PHP you would generate these like so:
<?php
$parameter_value = url_encode('MTQ2NA==');
$facebook_url = "http://urbana.com.uy/core.php?m=amp&nw=" . $parameter_value;
?>
Then on your server, when you read the value, just perform a URL decode
<?php
$parameter_value = url_decode($_GET['nw']);
?>
Other languages will have similar tools to perform this. I realize its not exactly what you're asking for, but the "=" sign is not a url safe character, and sending it in a parameter is going to cause issues even if you can get the rewriting working.

.htaccess redirect without query string

I'm trying to redirect (note without query strings):
http://www.reviews.com/review/review_review.cfm?review_id=135223
to
http://www.reviews.com/
The current rule:
Redirect 301 /review/review_review.cfm http://www.reviews.com/
Causes the original URL to redirect to http://www.reviews.com/?review_id=135223
Based on a few stack questions here and here, I should be able to add a ? to the Redirect rule as below:
Redirect 301 /review/review_review.cfm http://www.reviews.com/?
but this redirects to http://www.reviews.com/?. The trailing ? remains. How do I get rid of this it's killing me.
You can't get rid of the extraneous ? using mod_alias. The ? will prevent a query string from getting appended but with mod_alias, it unfortunately includes a ? as part of the redirect. However, mod_rewrite won't, because adding the ? to the end, which tells it to not include any existing query string, works the same way as mod_alias, but after that it gets processed again by mod_alias, minus the trailing ?, so the end result is no trailing ? at the end of the URL. So something like:
RewriteEngine On
RewriteRule ^/?review/review_review.cfm$ http://www.reviews.com/? [L,R=301]
And this would replace the Redirect statement.

How to redirect page with query-string to external webpage using APACHE RewriteRule

I am trying to redirect a login page to an external security service. This service, after validating the credentials, will then return user back to the originating page using the referrer url, as in the following example:
http://{IP NUMBER}/MyWiki/index.php?title=Special:UserLogin&returnto=Main_Page
or any call to a page in the site containing Special:UserLogin in the query_string needs to be redirected to:
https://login.security.server.com/test/UI/Login?service=DSSEC&goto=http://{IP NUMBER}/MyWiki/index.php/Special:UserLogin
I have been testing with RewriteCond and RewriteRule without any luck.
You want something like this?
RewriteEngine On
RewriteCond %{REQUEST_URI} Special:UserLogin [OR]
RewriteCond %{QUERY_STRING} Special:UserLogin
RewriteCond ?#%{QUERY_STRING} ([^#]+)#([^#]+)
RewriteRule ^ https://login.security.server.com/test/UI/Login?service=DSSEC&goto=http://%{SERVER_ADDR}%{REQUEST_URI}%1%2 [L,B,NE]
Ok, this is going to seem a little confusing, but here's what's going on.
Check if Special:UserLogin is in the request URI or the query string.
Create backreference matches for the ? mark, the URI and the query string (this is very important)
Redirect the request to https://login.security.server.com/test/UI/Login, but using the back references from the previous condition to build the goto= param, and using the B flag, which URL encodes the backreferences. This way, the result is an entire URL, along with query string, that's been URL encoded. (The NE flag is there to make sure the % signs themselves don't get double encoded).
With these rules, a request for:
/MyWiki/index.php?title=Special:UserLogin&returnto=Main_Page
Will get redirected to:
https://login.security.server.com/test/UI/Login?service=DSSEC&goto=http://123.45.67.89/MyWiki/index.php%3ftitle%3dSpecial%3aUserLogin%26returnto%3dMain_Page
As you can see, the query string ?title=Special:UserLogin&returnto=Main_Page gets encoded into %3ftitle%3dSpecial%3aUserLogin%26returnto%3dMain_Page, so that the login.security.server.com doesn't mistake it for its own query string. Instead, their login service will see the goto parameter as:
http://123.45.67.89/MyWiki/index.php?title=Special:UserLogin&returnto=Main_Page
entirely intact.

Redirect URL using Query String parameter in URL

I have a bunch of URLs from an old site that I recently updated to EE2. These URLs look like this:
http://www.example.com/old/path.asp?dir=Folder_Name
These URLs will need to redirect to:
http://www.example.com/new_path/folders/folder_name
Not all Folder_Name strings match up with folder_name URL segments, so these will most likely need to be static redirects.
I tried the following Rule for a particular folder called "Example_One" which maps to a page on the new site called "example1":
Redirect 301 /old/path.asp?dir=Example_One
http://www.example.com/new_path/folders/example1
But this doesn't work. Instead of redirecting I just get a 404 error telling me that http://www.example.com/old/path.asp?dir=Example_One cannot be found.
EDIT:
There's a secondary problem here too which may or may not be related: I have a catch-all rule that looks like this:
redirect 301 /old/path.asp http://www.example.com/new_path
Using the rule, requests like the first one above will be redirected to:
http://www.example.com/new_path?dir=Folder_Namewhich triggers a 404 error.
Just had to scour Google a bit more to find the proper syntax for mod_rewrite.
Using the example from above:
RewriteCond %{QUERY_STRING} ^dir=Example_One$
RewriteRule ^/old/path\.asp$ /new_path/folders/example1? [R=301,L]
This fixes both of the problems above -- as to why EE is 404ing with one parameter in the Query String, that problem is still unsolved, but this works as a workaround to that problem.
You can also redirect URLs to a specific page where the parameter may have a different value each time. One example of this is Google UTM Campaign tracking (in situations like this where the tracking query string triggers a 404):
Link: http://www.example.com/?utm_source=xxx&..... (triggers 404)
Should Redirect to: http://www.example.com
RewriteCond %{QUERY_STRING} ^utm_source=
RewriteRule ^$ http://www.example.com? [R=301,L]
Note: This will only redirect those requests to the homepage, as defined by ^$. If you want to redirect utm requests to a different page, you'll need to change the first part of that RewriteRule.