301 redirect rule
-
Hi there,
I have a website that has hundreds of links with a "question mark" at the end of URLs. For example:
http://www.domain.com/directory/page.html?
http://www.domain.com/directory/another-directory?
http://www.domain.com/directory/yet-another-directory/?I'm want to place a wildcard redirect on the .htaccess file but don't know what exactly to add. Ideally I want the URLs above to be:
http://www.domain.com/directory/page.html
http://www.domain.com/directory/another-directory/
http://www.domain.com/directory/yet-another-directory/Any help is most appreciated.
Thanks
Issa -
Hmmmm...you shouldn't need the RewriteCond. The regex for the rule itself merely needs to end in ?$, indicating that the ? must be the last character on the line.
And your rule looks to me like it will APPEND a question mark, not remove it!
I haven't tested this, but this should work as a blanket rule for all files and folders:
RewriteRule ^(.*)?$ /$1 [L,R=301]
-
Hi again,
Thank you for all this so far, however, it still doesn't help me very much. If i will need to follow the same system I will need to create a redirect line for each directory level and variation level I have on the website. Please not that in my initial question i said I have hundreds of cases of this issue.
I have discussed this issue with friends who came up with a solution that I thought i should share with you guys here. The redirect code should be:
RewriteCond %{THE_REQUEST} ^(GET|HEAD|POST)\ /[^\ ?]*?($|\ )
RewriteRule ^(.*)$ /$1? [L,R=301]
The first line ensures that the rule will only deal with question marks that appears at the end of the URL not the middle (as you kindly mentioned before). The second line then removes the question mark.
Plus, its just 2 lines, 1 rule which is a much shorter format
I hope this will be useful to someone else, it surely has saved me a lot of trouble, its now implemented and works perfectly.
Issa
-
Donford's correct. The "." matches any character; the "*" says 0 or more times; the $ means end of line.
-
the .*$ is the wildcard you can redirect on match keyword or directory.
Here is a good site that helps with this, special note the "Wildcard Redirect" section.
The question mark is usually an indication that the urls are dynamic if that is the case you may also want to review this helpful post on seOverflow.com about dynamic urls.
-
Not sure how the status of this question has changed to "Answered" but no thats not the right answer I'm afraid. What I was asking for is a wildcard redirect not redirect for the three examples i provided.
As mentioned i have hundreds of pages that require this change.
Issa
-
Donford's answer looks right to me, but I'd test it on your particular server as well.
My experience with .htaccess is that the various test tools don't always perform exactly the same as the server itself; also different webserver software (even different versions of Apache!) seem to have different results for the same patterns.
So...it's not always even sufficient to test on your development server.
-
This should work..
Test it out and see if we're on the right track.
RewriteEngine on
RedirectMatch 301 ^page.html?.$ http://www.domain.com/directory/page.html
RedirectMatch 301 ^/another-directory?.$ http://www.domain.com/directory/another-directory/
RedirectMatch 301 ^/yet-another-directory/?.*$ http://www.domain.com/directory/yet-another-directory/
Got a burning SEO question?
Subscribe to Moz Pro to gain full access to Q&A, answer questions, and ask your own.
Browse Questions
Explore more categories
-
Moz Tools
Chat with the community about the Moz tools.
-
SEO Tactics
Discuss the SEO process with fellow marketers
-
Community
Discuss industry events, jobs, and news!
-
Digital Marketing
Chat about tactics outside of SEO
-
Research & Trends
Dive into research and trends in the search industry.
-
Support
Connect on product support and feature requests.
Related Questions
-
301 redirect with DNS?
Quick question. Is it possible to 301 redirect a non-www to www. (properly in terms of SEO) with DNS (C Name, A name, or other) ..have searched around and found conflicting information. Would like to know a definite answer. I usually implement all 301 redirects with htaccess. However have a client situation where we only have access to the CMS, but which does have DNS settings. thanks in advance, Greg
Intermediate & Advanced SEO | | GregDixson0 -
What to do about old urls that don't logically 301 redirect to current site?
Mozzers, I have changed my site url structure several times. As a result, I now have a lot of old URLs that don't really logically redirect to anything in the current site. I started out 404-ing them, but it seemed like Google was penalizing my crawl rate AND it wasn't removing them from the index after being crawled several times. There are way too many (>100k) to use the URL removal tool even at a directory level. So instead I took some advice and changed them to 200, but with a "noindex" meta tag and set them to not render any content. I get less errors but I now have a lot of pages that do this. Should I (a) just 404 them and wait for Google to remove (b) keep the 200, noindex or (c) are there other things I can do? 410 maybe? Thanks!
Intermediate & Advanced SEO | | jcgoodrich0 -
301 redirect a old site that has been "dead" for a while?
Hi guys, A quick question. I have a client who has an old business website that had some great links (Forbes.com, CocaCola.com, etc). The problem is that he knew nothing about SEO and let the hosting expire. He still owns the domain, but the site is no longer listed in Google. He did no SEO, so I am not worried about being hit by any artificial anchor text penalties, since the links are as natural as it gets. So my questions is, would there be any benefit from 301 redirecting that site to his new business? The new business is in almost exactly the same niche as the old site. I am thinking of 301'ing to a sub-page which will refer to his past venture with the old business, not to the homepage of the new site. Thanks in advance for your help.
Intermediate & Advanced SEO | | rayvensoft0 -
How to decide on which site to 301 redirect
Hi there I'd like your opinions please! My client currently has their website at not-very-good-url.it which has a really good link profile they also have duplicate sites at: much-better-brand-name-url.it and much-better-brand-name-url.com but both these other sites have only a handful of links in. How important do you think a better brand url is? And therefore do you think it would be better to 301 to a better brand URL and take the risk that the link profile will get hit? Or leave the main site where it is and 301 the other two to it? Many thanks
Intermediate & Advanced SEO | | Chammy0 -
301 redirect or Link back from old to new pages
Hi all, We run a ticket agent, and have multiple events that occur year after year, for example a festival. The festival has a main page with each event having a different page for each year like the below: Main page
Intermediate & Advanced SEO | | gigantictickets
http://www.gigantic.com/leefest-tickets (main page) Event pages:
http://www.gigantic.com/leefest-2010-tickets/hawksbrook-lane-beckenham/2009-08-15-13-00-gce/11246a
http://www.gigantic.com/leefest-2010-tickets/highhams-hill-farm-warlingham/2010-08-14-13-00-gce/19044a
http://www.gigantic.com/leefest-2011-tickets/highhams-hill-farm-warlingham/2011-08-13-13-00-gce/26204a
http://www.gigantic.com/leefest-2012-tickets/highhams-hill-farm-warlingham/2012-06-29-12-00-gce/32168a
http://www.gigantic.com/leefest-2013/highhams-hill-farm/2013-07-12-12-00 my question is: Is it better to leave the old event pages active and link them back to the main page, or 301 redirect these pages once they're out of date? (leave them there until there is a new event page to replace it for this year) If the best answer is to leave the page there, should i use a canonical tag back to the main page? and what would be the best way to link back? there is a breadcrumb there now, but it doesn't seem to obvious for users to click this. Keywords we're aming for on this example are 'Leefest Tickets', which has good ranking now, the main page and 2012 page is listed. Thanks in advance for your help.0 -
Quoestion about 301 redirect
Hey, I have interesting questions regardin 301 redirect (At least I think it's Interesting:) ) So i have this websites that compares different lenders, url below 🙂 If you go to the homepage then the first thing you see is different loan amounts in 50-99 euro range. Also you can check out different loan amounts like 100-149€, 150-199€, 200-249€ and so on. For now i have used 301 redirect and Noindex and Nofollow for all the different "loan amounts" urls. Examples below etc Is it a good idea to use 301 on all such pages to point to the homepage?
Intermediate & Advanced SEO | | TauriU0 -
Are there any negative effects to using a 301 redirect from a page to another internal page?
For example, from http://www.dog.com/toys to http://www.dog.com/chew-toys. In my situation, the main purpose of the 301 redirect is to replace the page with a new internal page that has a better optimized URL. This will be executed across multiple pages (about 20). None of these pages hold any search rankings but do carry a decent amount of page authority.
Intermediate & Advanced SEO | | Visually0 -
Keyword-Rich Domains - Redirect?
Hi, Mozzers- I have a client that has a bunch of pretty nice keyword-rich domain names. Their traffic and rankings are good. They provide legal services in the Chicago area. I have lots of good content that I could use to start a blog using a domain like keyword,keyword-blog.com. Good idea? Currently I have a resources area on their website but feel like this area could be getting a little bloated and some news-related stuff isn't really appropriate. 2 Questions: Should I use one of the decent domains for a blog and build up the rankings, traffic, and link to the main site? Or is this lots of work for little payout? Both sites would be hosted in the cloud. Some of the domain names are related to their name, others are keyword or geo-targeted. Would it be wise to setup 301 redirects going to their website? Pros/cons? If you need additional info, please PM me for details. Thank you, friends! LHC
Intermediate & Advanced SEO | | lhc670