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
-
Need advice on redirects
Hi, I have new web addresses for my subpages. None if them have external links. Should I do redirects to the new pages or just leave the old pages in 404 and let google crawl and rank the new page. I am asking because my current pages don’t have a good ranking and I am thinking starting with a clean url is better. Thank you,
Intermediate & Advanced SEO | | seoanalytics1 -
Is it best to 301 redirect or use canonical Url when consolidating two pages?
I have build several pages (A and B) with high quantity content. Page A is aged and gets lots of organic traffic, ranks for lots of valuable keywords, and has only internal links to this page. Page B is newer (6 months) and gets little traffic, ranks for no keywords, but has terrific content and many high value external links. As Page A and B are related to a similar theme, I was going to merge content from page B onto page A, but don't know which would be the best approach for handling the links going to page B. For the purposes of keep as much link equity as possible, is it best to us a 301 redirect from B to A or use a canonical URL from B to A?
Intermediate & Advanced SEO | | Cutopia0 -
Will 301 Redirects Slow Page Speed?
We have a lot of subdomains that we are switching to subfolders and need to 301 redirect all the pages from those subdomains to the new URL. We have over 1000 that need to be implemented. So, will 301 redirects slow the page speed regardless of which URL the user comes through? Or, as the old urls are dropped from Google's index and bypassed as the new URLs take over in the SERPs, will those redirects then have no effect on page speed? Trying to find a clear answer to this and have yet to find a good answer
Intermediate & Advanced SEO | | MJTrevens0 -
NGinx rule for redirecting trailing '/'
We have successfully implemented run-of-the-mill 301s from old URLs to new (there were about 3,000 products). As normal. Like we do on every other site etc. However, recently search console has started to report a number of 404s with the page names with a trailing forward slash at the end of the .html suffix. So, /old-url.html is redirecting (301) to /new-url.html However, now for some reason /old-url.html/ has 'popped up' in the Search Console crawl report as a 404. Is there a 'blobal' rule you can write in nGinx to say redirect *.html/ to */html (without the forward slash) rather than manually doing them all?
Intermediate & Advanced SEO | | AbsoluteDesign0 -
How long should I keep the 301 redirect file
We've setup an new site and many pages don't exist anymore (clean up done). But for many of them we have new pages with new url's. We've monitored the 404 and have now many URL's redirected with 301 (apache file). How long should we keep this in place? Checking all links manually to see of new url is in place of the old url (in google) is too much work. tx!
Intermediate & Advanced SEO | | KBC0 -
How to do a 301 redirect for url's with this structure?
In an effort to clean up my url's I'm trying to shorten them by using a 301 redirect in my .htaccess file. How would I set up a rule to grab all urls with a specific structure to a new shorter url examples: http://www.yakangler.com/articles/reviews/other-reviews/item/article-title http://www.yakangler.com/reviews/article-title So in the example above dynamically redirect all url's with /articles/reviews/other-reviews/item/ in it to /reviews/ so http://www.yakangler.com/articles/reviews/boat-reviews/item/1550-review-nucanoe-frontier http://www.yakangler.com/articles/reviews/other-reviews/item/1551-review-spyderco-salt http://www.yakangler.com/articles/reviews/fishing-gear-reviews/item/1524-slayer-inc-sinister-swim-tail would be... http://www.yakangler.com/reviews/1550-review-nucanoe-frontier http://www.yakangler.com/reviews/1551-review-spyderco-salt http://www.yakangler.com/reviews/1524-slayer-inc-sinister-swim-tail with one 301 redirect rule in my .htaccess file.
Intermediate & Advanced SEO | | mr_w0 -
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 -
301 redirect help
Hey guys, I normally work in WordPress and just use a 301 redirect plugin. I bought a site and rather than maintain two similar ones have decided to redirect one to the other. I am having trouble with the .htaccess file. Here is an example. These are two redirects: redirect 301 /category/models/next/2
Intermediate & Advanced SEO | | DanDeceuster
redirect 301 /category/models I want both of these URLs to redirect to the same URL of the new site. However, the /category/models is the only one working. It redirects to the new page just fine. The /category/models/next/2 is redirecting to nearly the same URL on the new site, only it is adding /next/2 to the end and that is bringing up a 404. Why is it adding /next/2 to the new URL? How can I fix this? There are several doing this. Help appreciated!0