How to pass URL Parameters through a .htaccess file - .htaccess

I need to transparently pass a URL parameter through a .htaccess 301 redirection but I am unfamiliar with how to code it.
For example, an Adwords clickthrough appends the following parameter to the landing page url:
&gclid=CKCPq62Sq6wCFY1S4god4FZd1g
Our Google landing page is being redirected like this:
Redirect 301 /old-page /new-page
(We don't want to edit our Google Ads as doing so would lose our existing stats. Thus the redirect..)
How do I preserve the above gclid parameter while redirecting in .htaccess?
Thanks,
Geoff

You can't handle dynamic query strings properly with standard htaccess 301 redirect. In order to do so you should use mod_rewrite:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule /old-page /new-page?%{QUERY_STRING} [R=301,L]
See, http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
NOTE: I'm not positive that RewriteCond is required, but it's there to prevent the page from redirecting over and over again. Depending on the rest of the layout of your site, you may need it... although in most cases it's not needed unless you're redirecting all requests.

Related

Changing my website from dynamic CMS to static HTML. Should I 301 redirect or use other methods?

I am changing my website from a dynamic CMS-system (Umbraco) to a static classic .HTML. It is the on the same domain, but the URL will change.
Example: The URL is changing from:
www.example.com/information
To:
www.example.com/info.html
My question is:
What is the best way to redirect while keeping the best SEO page rank.
I am thinking about 301 redirect through .htaccess, but I am not sure if I should redirect my new to .html urls to the old dynamic …/example - or the other way?
Or maybe there is a different better way?
I do have a fine 404.
Also I need the right redirect code for .htaccess - if that's the right way.
I hope you guys can help me out.
I haven't try anything out yet, because I don't wanna do 301 before the site go live.
You need to implement 301 redirects from the old URL to the new URL in order to preserve SEO and ensure that any "old" links that have been bookmarked or linked to from other websites still work.
Exactly how you implement the 301 redirect (either in your server-side script or in .htaccess) does not really matter. However, if you are moving to an entirely static site then .htaccess is likely the only option you have.
I am not sure if I should redirect my new to .html urls to the old dynamic …/example - or the other way?
You need to redirect from the "old" URLs to the "new" URLs that you are using/linking to on the new site. (It makes no sense to redirect the other way as that would just break everything!)
You can probably just use the simple mod_alias Redirect directive.
For example, to 301 redirect from /information to /info.html you could do the following:
Redirect 301 /information /info.html
Bear in mind that 301 redirects are cached persistently by the browser. To prevent caching issues it is advisable to test first with a 302 (temporary) redirect.
Have you considered keeping the same URLs? This would obviously negate the need for implementing redirects. You could employ URL-rewriting if the underlying file is called info.html. For example, using mod_rewrite:
RewriteEngine On
RewriteRule ^information$ info.html [L]
The above would internally rewrite a request for /information to info.html. The user only sees /information in the browser address bar, but info.html is served from your site.
Taking this further, it would be easier if the new "file" is simply the same as the old URL, just with a .html extension. For example, the URL is /information and the underlying file is information.html. You can then use a single rule to rewrite all your URLs. For example:
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^([^/.]+)$ $1.html [L]
The above assumes the old URLs do not contain additional slashes (ie. consist of a single path segment. In other words, all files are in the document root) and do not contain dots.
White
I finally got my page ready to go live, and i changed all my new URLS to the same name as the old URL, just with a .html extension - as u said. After that i used:
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^([^/.]+)$ $1.html [L]
And it works fine.
I do have a question about if a "RewriteRule ^(.*).html$ /$1 [L,R=301]" would be better? I mean both "/page.html" and "/page/" works, and this could mess with my former SEO ranking?
Also: what do u think of this:
RewriteCond %{THE_REQUEST} /([^.]+)\.html [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^ %{REQUEST_URI}.html [NC,L]
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
And by the way. Do you use "page.html" or just "/page" in your href to prevent the .html showing?
//MM

htaccess redirect from page with specific query string

I'm new to the rewrite rules in htaccess and need some help with the following:
Page I want to redirect from:
http://example.com/webinar/?confirmed
Page I want to redirect to when a user tries to access the above URL:
http://example.com/webinar-thanks
What makes this tricky is that I will still need to access the original URL with different query strings such as:
webinar/?stats
webinar/?console
And so on. I tried to do a 301 redirect but that redirects any attempt to access the /webinar page, regardless or the query string at the end, so I guess I need some rewrite syntax?
Try:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^confirmed$
RewriteRule ^webinar/?$ /webinar-thanks [L,R]

htaccess not redirecting because of '?'

I'm having a problem with my htaccess, we've moved a website and they're old website had a lot of duplicate pages that had a ?cat_id=88 etc... on them, I'm trying to redirect the page but it's not working, I've put the redirect code below, I'm already redirection the mantra.html to the /Mantra but the version with the ?cat_id=79 isn't redirecting, it's just ignoring everything after the ?
Redirect 301 /mantra.html?cat_id=79 http://www.website.co.uk/Mantra
Redirect only accepts paths, not paths with querystring. You could use mod_rewrite
RewriteEngine on
RewriteCond %{QUERY_STRING} ^cat_id=79$
RewriteRule ^mantra\.html$ /Mantra? [R=302,L]
change 302 to 301 once you get it working (301 are aggressively cached by browsers and make debugging a nightmare).
EDIT added a ? at the end to remove any querystring. Apache removes the ? if there is no other data in the querystring, so the end user will never see it.

HtaccessRules to redirect my website

Hi can anyone of you suggest me on how to redirect my page http://testsite.com/about.php to http://testsite.com/about/ by using htaccess rewrite rules.Without using any query string in the page url i need to rewrite it
I don't think you want a redirect but a rewrite. A redirect means informing the client to requery for the redirected url, whereas rewriting means telling the server to interpret an url as being some other url.
and you dont' want to rewrite about.php to /about/ but the other way around. You want your users to type in /about/ and that url to be handled by /about.php
and this is done by:
RewriteEngine on # this line enables rewrite
RewriteRule /about/$ /about.php
RewriteRule /about$ /about.php
I'm not sure if both of the above are actually needed or just one (I don't have access to an apache right now to test)
In the slight chance that you actually do want a redirect as you wrote, use this free redirect generator: http://www.htaccessredirect.net/ (it's easier than learning all the quirks of the conditions to match up)
in your case that will be:
Redirect 301 /about.php /about/

301 redirect urls

I'm trying to redirect an old url to a new one using 301
I need an example of RewriteQueryString for the following 301? http://www .example.com/search/?depId=1&typeCatId=1 to the following http://www.example.com/mens/clothing
So when I type in the long URL in the browser, I am redirected to the new, shorter URL
Any ideas?
RewriteEngine On
RewriteRule ^search/\?depId=1&typeCatId=1$ /mens/clothing [R=301]
^ Try that.
You could use mod_rewrite either in your .htaccess file or the apache configuration. You might take a look at the RewriteMap feature if you are going to have a lot of different departments, etc. to map. Using the [R] flag after the RewriteRule will cause the browser to redirect instead of just being an internal redirect. Using [R=301] will make it a 301 redirect.

Resources