htaccess not redirecting because of '?' - .htaccess

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.

Related

Redirect targeting the beginning and end of an old url

Im making redirects of the old pages at the same domain, using htaccess. There are too many urls, and I want to create a general rule to be more practical. How can I create a htaccess rule, that redirects anything that starts with the directory "limitation/" and ends with .html?
Here is my present code, the last line is not ok yet, it does nothing at all. What should I do?
RewriteEngine On
Redirect 301 ^limitation/(.*).html$ https://example.com/news/
I was using redirect 301, with some wrong syntax. Here is hat worked:
RewriteRule ^limitation/(.+)$ https://nagoyaaqua.jp/news/ [R=301,L]

How to 301 redirect example.com/http://cnn.com/page to example.com/cnn/page?

I'm setting up a project where users can get information about specific pages.
How can I redirect urls in htaccess that look like:
http://www.example.com/http://www.cnn.com/article1.html
to:
http://www.example.com/cnn/article1.html
It is ok if the htaccess specifies cnn and not all dynamic web addresses.
I tried using 301 directory redirects but I think the "http://" part of it is confusing it.
I tried using 301 directory redirects like the following code, but the http:// part is still confusing it:
RewriteRule ^http://youtube.com/(.*)$ /youtube/$1 [L,R=301]
RewriteRule http://www.example.com/http://youtube.com/(.*)$ /youtube/$1 [L,R=301]
This code below works properly to redirect example.com/youtube.com/watch?v=videoid to example.com/youtube/watch?v=videoid:
RewriteRule ^youtube.com/(.*)$ /youtube/$1 [L,R=301]
but if the user types in a link with http:// in it then the server cannot currently redirect properly.
Even better, if you can remove the /http:// and /https:// altogether from http://example.com/http://url.com and http://example.com/https://url.com to redirect to http://example.com/url.com/ then the redirect code that I have working in the youtube example will fix the issue for all of the domains.
How can I do this?
Thank you!

301 redirect subdirectory and anything in it

I bought an old site that apparently used some kind of a CMS, maybe wordpress.
Now it's a simple html site. I want to redirect www.example.com/main and everything in that directory to root. In other words, if a url is example.com/main/a_bunch_of_garbage that should 301 redirect to the homepage. Here is what I have but it doesn't quite work.
Options +FollowSymLinks
RewriteEngine on
RedirectMatch 301 ^/main/(.*)$ http://www.example.com
If the url doesn't contain any weird characters like ? or = then it works. But for example, if I go to www.example.com/main/index.php?option=com_content&view=post&id=84&Itemid=982/
Then although it does redirect to the homepage, in the address bar, I still see the entire URL. Can I fix this?
I also just remembered another problem I had. If I put example.com/main (without forward slash after main) the redirect doesn't work. It only works if I put example.com/main/ (with forward slash)
You need to use mod_rewrite rules only and avoid mixing RedirectMatch here. Have your rules like this in your root .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^main(/.*)$ $1? [L,NC,R=301]
? in the end is used to strip off any existing query string.

How to pass URL Parameters through a .htaccess file

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.

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