Rewrite url to remove gclid query - .htaccess

I am trying to remove google's gclid tracking parameter from my urls. After searching around the internet it appears that I need to use url rewriting to solve the problem.
I am using IIS6 as a server so rather than .htaccess I am using isapi rewrite filter which is supposed to work the same way.
Ideally I would like to make this:
http://www.example.com/default.asp?parameter=stufftokeep&gclid=alotofrandomstuff
Become this:
http://www.example.com/default.asp?parameter=stufftokeep
no matter what comes after "gclid"
I was able to find this here on SO but replacing "tag" with "gclid" did not work, and keeps breaking the page.
Has anyone run into this issue with gclid parameter before who might be able to offer a solution?

Try:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)(?:^|&)gclid=[^&]+(&.*)?$
RewriteRule ^(.*)$ /$1?%1%2 [L,R=301,NE]

In case anyone else has this issue with gclid parameter on IIS6 server using isapi rewrite filter v2 here is the code that solved the issue.
RewriteRule ^/([^?]+)\?(.*)(?:^|&)gclid\=[^&]+(\&.*)?$ /$1\?$2$3 [I,RP,L]

Related

Internal rewrite to remove directory from url with htaccess

I've searched and tried many tutorials and problem solutions but couldn't achieve what I want.
If user open domain.com/xxx.php he see the content of domain.com/hosting/xxx.php
This
RewriteRule ^xxx.php$ /hosting/xxx.php
Works perfectly what i want to do, but there are over 150 pages in hosting directory and writing rules for each page does not seem right? any possible way to do it dynamically?
I tried this solution it works but it try to find those pages which are currently at root domain.com/xxx.php inside domain.com/hosting/
I search slimier questions have been asked many times before but non of the answer worked for me.
You need to also take care of reuests to the shortened, rewritten URL:
RewriteEngine on
RewriteRule ^/?hosting/(.*)$ /$1 [END,NC,R=302]
RewriteRule ^/?(.*)$ /hosting/$1 [END]

I am not able 301 redirect domain.tld/?cur=usd to domain.tld

I try to redirect domain.tld/?cur=usd to domain.tld (there are many curencies, this is only example of one currency - we do not use anymore this solution).
I need to redirect only home with parameter to home without parameter. The other urls worked for me, I'm just having trouble getting work with that one.
I try to search and use online generators but none of the solutions work.
Here is what I am trying:
RewriteCond %{QUERY_STRING} (^|&)cur\=(.*)($|&)
RewriteRule ^$ /? [L,R=301]
// update
before this rule I have only
#bof redirects
RewriteEngine enabled
...and then there are redirects for other URLs, but I tested this rule separately first and the result was the same...
It not redirect me.
Thanks for the help and maybe an explanation of what I'm doing wrong.
RewriteCond %{QUERY_STRING} (^|&)cur\=(.*)($|&)
RewriteRule ^$ /? [L,R=301]
As mentioned in comments, this should already do as you require, providing there are no conflicts with other directives in the .htaccess file.
However, the regex in the preceding condition is excessively verbose for what you are trying to achieve (ie. just testing for the presence of the cur URL parameter).
If you simply want to check for the cur URL parameter anywhere in the query string then the regex (^|&)cur= would suffice (and is more efficient). No need to backslash-escape the literal =. And if the URL parameter always appears at the start of the query string then just use ^cur=.
I found the problem - it was something with the hosting, after a reboot everything started working as expected.
So I can confirm that this rule is fine.
Sorry for question.

Not matching correct parameter

I'm trying to use mod_rewrite to overwrite a subdomain to a set of query parameters.
For example, sub.domain.com should direct to index.php?option=com_pages&layout=swf, while sub.domain.com/about should direct to index.php?option=com_pages&layout=swf/about.
I currently have this:
RewriteCond %{HTTP_HOST} ^sub\.domain\.com
RewriteRule ^(.*)$ index.php?option=com_pages&layout=swf/$1 [L,NC,QSA]
It works correctly for sub.domain.com, but for sub.domain.com/about, I'm directed to index.php?option=com_pages&layout=swf/index.php and I'm not sure why.
I've ready a few questions on here, a few tutorials and still can't seem to see what I'm seeing. Any assistance is appreciated.
I'm on Apache 2.2 if that makes a difference.
EDIT: I've taken a look at my server and I'm getting the following for $_SERVER:
[REDIRECT_QUERY_STRING] => option=com_pages&layout=swf/about
[QUERY_STRING] => option=com_pages&layout=swf/index.php&option=com_pages&layout=swf/about
I'm not sure why its being concatenated like that.
I was using the QSA-flag which appends the query string.

.htaccess dynamic to static URL

I'm trying to make my dynamic URL's into static looking URL's.
This is a typical URL that I now have:
http://www.somedomain.com/design/index.php?p=about
I would like it to be: http://www.somedomain.com/about
So far, I've gotten it to look like this: http://www.somedomain.com/design/about.html
This is the Rewriterule I'm using: RewriteRule ^([a-z]+).html$ index.php?p=$1 [L]
How would I modify it so it would look like this: http://www.somedomain.com/about?
Thanks for any/all help!!!
Very much appreciated!
Using rewrite rules to give 'static' URI is NEVER a good idea.
A few other ideas you can use:
Make the 'about' page a directory (folder) with a file called index.php or index.html in it. This way the URL shows http://example.com/about/ and the information you wish can still be displayed as needed.
Use the POST method instead of GET methods. This will display as http://example.com/about.php (Note: there is no ? or other parameters behind that.)
Utilize both methods to give a 'seamless' URI on page transitions.
Rick, you're on the right track. You need to read the Apache rewrite documentation. For your docroot/.htaccess start it with:
RewriteEngine On
RewriteBase /
Then generalised version of your rule:
Rewrite Rule ^(\w+)$ index.php?p=$1 [L]
This will rewrite any requests which are for a word string to index.php. You need to be aware that the rewrite engine rescans the .htaccess file if a match has occured so you need to make sure that you don't create a loop. In this case the replacement string has a "." in it and the pattern doesn't, so this won't occur, but for more complex cases you may need to 'guard' the rules with one or more RewriteCond statements. Again, read the Apache documentation.

.htaccess and rewrite of URL

We have the following example "ugly" URL:
https://some.uglyurl.com/directory/test.jsp?hotelid=1111&rateplanid=33333
we need to direct our customers to the above URL using our own domains URL as the address - so it would look something like:
https://www.PrettyURL.com/reservations?hotelid=1111&rateplanid=33333
The idea being that the address our customers "see" is a nice looking "familiar" URL to them. Is this possible in .htaccess? We would tack on various variables AFTER the test.jsp in the ugly URL - so it can't just be a fixed set of variables.
many thanks for any help.
If you were just using plain HTTP, you could set up the pretty URL server as a proxy that passes every request to the ugly URL server and the response back to the client:
RewriteCond %{HTTP_HOST} ^pretty\.example\.com$
RewriteRule ^reservations$ http://ugly.example.com/directory/test.jsp [L,P]
But as you’re using HTTPS, it is not possible without getting an error message, that the certificate’s host name is not correct.
This is the code that worked for me:
RewriteCond %{HTTP_HOST} ^www.prettyurl.com$
RewriteRule ^reservations$ https://uglyURL.com/istay/istay.jsp?%2 [QSA,L]
It works exactly as I needed it.
Many thanks to Gumbo and others for all their help.

Resources