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 from Authoritative but Loosely-Related Domain
We acquired a health-related blog about a year ago with good domain authority and a pretty strong link profile (TF ~40). We have been publishing good relevant content in it but it's not really paying dividends and we are considering doing a 301 to our money site, which is focused primarily on senior issues but has a lot of health-related content. The question is - with the two domains only being loosely related in subject matter, do we stand to harm our main site by redirecting from the other domain?
Intermediate & Advanced SEO | | sa_787040 -
301 redirection help needed!
Hi all, So if we used to have a domain (let's say olddomain.com) and we had a new site created at newdomain.com how do we properly setup redirects page to page. Caveat, the urls have changed so for instance the old page oldomain.com/service is now newdomain.com/our-services on the new site. Do we need to have hosting on the old site? Do we need to setup individual 301s for each page corresponding to the new page? Just looking for the easiest way to do this CORRECTLY. Thanks, Ricky
Intermediate & Advanced SEO | | RickyShockley3 -
Suggested approach (support) for 301 redirects in event of an acquisition
If an agency has recently been acquired by a new organisation, it will need to be redirected to the new organisation's website as soon as possible. We are aware of the need to 301 redirect all pages (domain authority) across to the current domain of the new organisation's website. The new organisation has less pages than our Agency site however, so we cannot point 301 redirects at page level. Would you therefore advise, A, B or C?: A) Redirecting all pages including all blog posts/services pages etc across from the agency site to the new organisation's domain? * new organisation does not have /blog or /services pages. -Will we lose authority if redirecting from pages of our agency site to the new organisation's top level domain? B) Ensure that the new organisation secures hosting of the agency website, and place a holding page on the Agency website directing visitors through to the new organisation for the interim, until we have a /blog, /services page on the new organisation's site? C) Place 301 redirects from agency across to new organisation, and look moving forward (when pages have been put in place on new organisation website) to retrospectively repoint 301 redirects from top level domain of new organisation's site to the new pages which have just been created on the new organisation's site? Any pointers here would be appreciated. Thanks!
Intermediate & Advanced SEO | | Tangent0 -
Is it possible to avoid redirect of penalties for 301 forwards?
We have been doing a good amount of competitive research lately and have noticed sites that have been changing their TTLD quite often to escape manual penalties / DCMA filings. An example evolution: brandterm.com -> brandterm.bz -> brandterm.me These competitors are able to quickly rank for money keywords in the top 3 soon after another domain switch. What we have noticed is that while its obvious they received Google penalties they continue to 301 redirect the old domains to the new ones. We have experienced first hand that penalties travel along domains with 301 redirects. Does anyone have an explanation how these companies are able to achieve quickly high volume of organic search while 301-redirecting from burnt domains? The only option I see is to disavow all previous domains in GWT to be able to employ 301 redirects without risking carrying over the penalty. Are there other theories ppl can think of? T
Intermediate & Advanced SEO | | petersocapro0 -
301 redirect subdirectory to new domain
I'm planning on using 301 redirects to spin out a subdirectory of my current website to be its own separate domain. For instance, I currently have a website www.website.com and my writers write tech news at www.website.com/news. Now I want to 301 redirect www.website.com/news to www.technews.com. Will this have any negative impact on SEO? What are some steps that I can take to minimize these impacts?
Intermediate & Advanced SEO | | Chris_Bishop1 -
301 doesn't redirect a page that ends in %20, and others being appended with ?q=
I have a product page that ends /product-name**%20** that I'm trying to redirect in this way: Redirect 301 /products/product-name%20 http://www.site.com/products/product-name And it doesn't redirect at all. The others, those with %20, are being redirected to a url hybrid of old and new: http://www.site.com/products/product-name**?q=old-url** I'm using Drupal CMS, and it may be creating rules that counter my entries.
Intermediate & Advanced SEO | | Brocberry0 -
How to stop Google crawling after 301 redirect?
I have removed all pages from my old website and set 301 redirect to new website. But, I have verified old website with Google webmaster tools' HTML verification file which enable me to track all data and existence of pages in Google search for my old website. I was assumed that, Google will stop crawling and DE-indexed all pages after 301 redirect. Because, I have set 301 redirect before 3 months. Now, I'm able to see Google bot activity on my website with help of Google webmaster tools. You can find out attachment to know more about it. How can it possible & How Google can crawl removed pages? You can see following image to know more about it. First & Second
Intermediate & Advanced SEO | | CommercePundit0 -
301 redirect for ip address in SERPs
Hi, I've recently had the misfortune of my site's ip address being crawled and indexed by Google, which is causing some duplicate content issues. Due to the nature of the site we're not able to implement a canonical tag to fix this at present. Would a 301 redirect do the trick, and if so, could someone point me to what I'd need to add to our .htaccess file? Many thanks Chris
Intermediate & Advanced SEO | | ChrisHillfd0