Redirect 301 for Apache Sub directory and query string combination

Redirect 301 for Apache Sub directory and query string combination - .htaccess files are extremely useful in many cases for users who either do not have root permissions or for users who simply aren't comfortable in making changes in their web server's configuration file. Trying to debug .htaccess not working isn't always the easiest thing to do, however, hopefully by checking the discuss below mentioned about htaccess, redirects, 301-redirect, mod-rewrite, .htaccess common problems as well as the troubleshooting tips, you'll have a better grasp on what you may have to modify to get your .htaccess file running smoothly.Problem :


I am getting 404 errors in Google Search Console for URLs like the following:



http://www.example.com/newsite12121212121212/eg/st_tour/perma/
http://www.example.com/newsite12121212121212/eg/st_activity/extreme-biking/
http://www.example.com/newsite12121212121212/eg/st_activity/extreme-biking/?currency=EUR
http://www.example.com/newsite12121212121212/eg/st_rental/new/?currency=EUR
http://www.example.com/eg-login/?location_id=1680&s=France&currency=ALL&orderby=price_desc


I want to redirect all of these to the home page of my site as the pages do not exist.



Problem # 1: I want to redirect any page that is under /newsite12121212121212/ (including any subdirectory) to the home page.



Problem # 2: ?currency= some thing, take it back to home page. I tried



RewriteEngine On
RewriteCond %QUERY_STRING ^currency=$
RewriteRule ^$ https://www.example.com/? [R=301,L]


It's working for the http://www.example.com?currency=EUR URL but fails on http://www.example.com/eg-login/?currency=EUR.


Solution :

If you want to redirect any page to the home page when it has a currency parameter, you have to change your regular expression that matches the path portion. Currently you are trying to use ^$ which matches an empty path. .* would match "any path":



RewriteEngine On
RewriteCond %QUERY_STRING currency=
RewriteRule .* https://www.example.com/ [R=301,L,QSD]


You could have another rewrite rule that for the directory:



RewriteRule /?newsite12121212121212.* https://www.example.com/ [R=301,L]


As we have said in the comments, it isn't good for SEO to redirect to the home page. Google considers home page redirect to be "soft 404" errors. It will treat them as if you return "404 Page Not Found" status. Redirecting to the home page won't make the errors in Google Search Console go away.



If you actually have a page for st_tour/perma, you should redirect to it instead of the home page. It might require more redirect rules, but both users and search engines will be happier.



If you have never had such a page, then don't try to claim the URL. It may be caused by spam, malware, or a previous spammy site running on your domain name. If you claim those URLs you could be inviting penalties from search engines. Having 404 errors in your crawl report on Google Search Console for pages that shouldn't exist won't hurt your site in any way. John Mueller from Google says so here: https://plus.google.com/+JohnMueller/posts/RMjFPCSs5fm


Additionally, if you would like to do some further testing, give the htaccess tester tool a try. It allows you to specify a certain URL as well as the rules you would like to include and then shows which rules were tested, which ones met the criteria, and which ones were executed.

Comments

Popular posts from this blog

Rewrite in Mediawiki, remove index.php, .htaccess

.htaccess rewrite wildcard folder paths from host

Using .htaccess to set a cookie and 301 redirect