Help needed please with 301 redirects in htaccess file.
-
In summary, we're currently having issues with our htaccess file. 301 redirects are going through to the new described URL but in addition the new URL is followed by a ? and the old URL. How can we get rid of the ? and previous URL so they don't appear as an ending.
None of the examples we've found re this issue online appear to work. Can anyone please offer some advice? Can we use a RewriteRule to stop this happening?
Here's a summary of the htaccess file
REDIRECT CODE BEGINS HERE
LONG LIST OF REDIRECTS, which appear to be set up perfectly fine.
REDIRECT CODE ENDS
DirectoryIndex index.php
<ifmodule mod_rewrite.c="">RewriteEngine On
Options +FollowSymLinks
DirectoryIndex index.php
RewriteEngine On
RewriteCond $1 !^(images|system|themes|pdf|favicon.ico|robots.txt|index.php) [NC]
RewriteRule ^.htaccess$ - [F]
RewriteRule ^favicon.ico - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]</ifmodule>DirectoryIndex index.php
-
Hi Cyrus,
Thank you so very much. Success! I can't tell you how delighted and grateful I am. I have spent hours over the Easter weekend trying to fix it, but to no avail. You are a god in my eyes! You have made my day and halted the rash of broken/duped links emanating from our website.
Thank you, thank you, thank you.
Very best,
Peter
-
Ha! I always get myself into trouble when trying to answer these .htaccess questions. Someday I'll learn.
I'm not an expert, but here's one thing you can try. Make sure to backup your file before you attempt these, in case you break something you want to be able to quickly back it up. My suspicion is there are conflicting rules in the file that are causing the wrong redirection.
-Try changing your rewrite rules to the following format. The "L" indicates this is the last directive, and place no more rewriting rules on this condition.
RewriteRule ^tours2006.html http://www.petersommer.com/escorted-archaeological-tours/ [L,R=301]
If this doesn't work, let me know and I'll try to find someone who knows more.
-
Many thanks Cyrus. Sorry to hear you're confused! Thank you for offering to help.
There are redirects of a number of old pages. Here's a sample of them.
redirect 301 /index.html http://www.petersommer.com/
redirect 301 /writing_index.html http://www.petersommer.com/travel-writing/
Unfortunately I'm not sure what isn't working or what needs to be done to fix it. I wish I did! I assume it must be something in the Rewrite Rules or we need to add in some Rewrite Rules to fix it.
All the redirects are unfortunately adding a ? and the old URL to the end of the new redirected URL.
All are following the same pattern - unfortunately.
If you can see any glitches in the Rewrite Rules or can identify something we need to add that will be super.
Thanks again, Peter
-
Hi Peter,
Would love to help out, but by this point in the thread I'm thoughtfully confused! Any chance we could start from the beginning? If you would like, please list
- exactly an example URL you are trying to redirect,
- The exact code that isn't working
- The end result of how it redirects.
Not promising I can help any more than Marisa, and maybe my brain just hurts right now, but I'm willing to give it a shot. Thanks.
-
Thanks for trying Marisa. Appreciate your time and effort. Best wishes, Peter
-
I'm not sure if I can help further or not, since I'm certainly not an Apache coding expert. Right off the bat it seems like you may have a dynamic URL redirect already in place, so when you add anther redirect, it's in a sense, putting a double redirect on the URLs.
I wish I could help further, but one thing I will say is that the only way I've found redirects from multiple directories to work is to put the redirects in each corresponding directory's .htaccess file, and not the root. I wish there was an easier way to do it, and if there is, I hope someone will chime in here and enlighten us.
Sorry I couldn't be of further assistance.
-
Thanks so much for the quick response Marisa. Apologies if I've confused in any way.
The 301 redirect of the gallery tombs is just one of a long list of redirects, hence 'other redirects...' I removed the rest just for ease.
The htaccess is in the main root. I just happened to leave a gallery page as an example of a redirect.
I wondered if there might be a Rewriterule that I could include in the file to solve this issue across the board rather than deal individually - there's about 100 redirects including the one mentioned.
If you can assist further that would be brilliant! I'd really appreciate it.
best wishes,
Peter
-
Are you putting the code in the .htaccess of the root directory of of the gallery directory? If you're putting it in the root, that is your first mistake. Try putting the following code in the .htaccess of the gallery directory:
RewriteEngine On
RewriteBase /RedirectMatch 301 (.*)kaunos-tombs-dalyan-photo.html$ http://www.petersommer.com/gallery/
Replace the .html with .php or whatever the extension of the kaunos-tombs-dalyan-photo file is.
One question, though. Is kaunos-tombs-dalyan-photo a directory or a file? meaning is your URL technically this: http://www.petersommer.com/gallery/?/gallery/kaunos-tombs-dalyan-photo.html or this: http://www.petersommer.com/gallery/?/gallery/kaunos-tombs-dalyan-photo/index.html
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
-
Is 301 redirect the only way when using Vanity URLs?
We have been using vanity urls for some of our pages. Mostly the pages that have a vanity URL have a long URL length. But now the problem is, the vanity URL is getting displayed on the search engine when the particular keyword related to the page is entered. I checked the google search console, the vanity URL is indexed and the original URL remains unindexed. What should I do? Is adding 301 redirect to the vanity URLs are solution? Since some of vanity URLs are not redirecting to the original. Some of the original pages are not getting traffic. Also, can using canonical tag help?
Technical SEO | | tejasbansode0 -
Selective 301 redirections of pages within folders
Redirection Puzzle - it's got me puzzled anyhow! The finished website has just been converted from an old aspx affair to a wordpress site. Some directory structures have changed significantly; there appears to be a load of older medical articles that have not been added back in and it sounds unlikely that they will be. Therefore unmatched old news articles need to be pointed to the top news page to keep hold of any link value they may have accrued. The htaccess file starts with ithemes security's code, Followed by the main wordpress block and I have added the user redirects to the final section of the htaccess file . I have been through the redirects and rewrites line by line to verify them and the following sections are giving me problems. This is probably just my aging brain failing to grasp basic logic. If I can tap into anybody's wisdom for a bit of help I would appreciate it. My eyes and brain are gone to jelly. I have used htaccesscheck.com to check out the underlying syntax and ironed out the basic errors that I had previously missed. The bulk of the redirects are working correctly. #Here there are some very long media URLs which are absent on the new site and I am simply redirecting visiting spiders to the page that will hold media in future. Media items refuse to redirect
Technical SEO | | TomVolpe
Line 408 redirect 301 /Professionals/Biomedicalforum/Recordedfora/Rich%20Media%20http:/kplayer.kcl.ac.uk/ess/echo/presentation/15885525-ff02-4ab2-b0b9-9ba9d97ca266 http://www.SITENAME.ac.uk/biomedical-forum/recorded-fora/ Line 409 redirect 301 /Professionals/Biomedicalforum/Recordedfora/Quicktime%20http:/kplayer.kcl.ac.uk/ess/echo/presentation/15885525-ff02-4ab2-b0b9-9ba9d97ca266/media.m4v http://www.SITENAME.ac.uk/biomedical-forum/recorded-fora/ Line 410 redirect 301 /Professionals/Biomedicalforum/Recordedfora/Mp3%20http:/kplayer.kcl.ac.uk/ess/echo/presentation/15885525-ff02-4ab2-b0b9-9ba9d97ca266/media.mp3 http://www.SITENAME.ac.uk/biomedical-forum/recorded-fora/ #Old site pagination URLs redirected to new "news" top level page - Here I am simply pointing all the pagination URLs for the news section, that were indexed, to the main news page. These work but append the pagination code on to the new visible URL. Have I got the syntax correct in this version of the lines to suppress the appended garbage? RewriteRule ^/LatestNews.aspx(?:.*) http://www.SITENAME.ac.uk/news-events/latest-news/? [R=301,L] #On the old site many news directories (blog effectively) contained articles that are unmatched on the new site, have been redirected to new top level news (blog) page: In this section I became confused about whether to use Redirect Match or RewriteRule to point the articles in each year directory back to the top level news page. When I have added a redirectmatch command - it has been disabling the whole site! Despite my syntax check telling me it is syntactically correct. Currently I'm getting a 404 for any of the old URLs in these year by year directories, instead of a successful redirect. I suspect Regex lingo is not clicking for me 😉 My logic here was rewrite any aspx file in the directory to the latest news page at the top. This is my latest attempt to rectify the fault. Am I nearer with my syntax or my logic? The actual URLs and paths have been substituted, but the structure is the same). So what I believe I have set up is: in an earlier section; News posts that have been recreated in the new site are redirected 1 - 1 and they are working successfully. If a matching URL is not found, when the parsing of the file reaches the line for the 1934 directory it should read any remaining .aspx URL request and rewrite it to the latest news page as a 301 and stop processing this block of commands. The subsequent commands in this block repeat the process for the other year groups of posts. Clearly I am failing to comprehend something and illumination would be gratefully received. RewriteRule ^/Blab/Blabbitall/1934/(.*).aspx http://www.SITENAME.ac.uk/news-events/latest-news/ [R=301,L] #------Old site 1933 unmatched articles redirected to new news top level page RewriteRule ^/Blab/Blabbitall/1933/(.*).aspx http://www.SITENAME.ac.uk/news-events/latest-news/ [R=301,L] #------Old site 1932 unmatched articles redirected to new news top level page RewriteRule ^/Blab/Blabbitall/1932/(.*)/.aspx http://www.SITENAME.ac.uk/news-events/latest-news/ [R=301,L] #------Old site 1931 unmatched articles redirected to new news top level page RewriteRule ^/Blab/Blabbitall/1931/(.*)/.aspx http://www.SITENAME.ac.uk/news-events/latest-news/ [R=301,L] #------Old site 1930 unmatched articles redirected to new news top level page RewriteRule ^/Blab/Blabbitall/1930/(.*)/.aspx http://www.SITENAME.ac.uk/news-events/latest-news/ [R=301,L] Many thanks if anyone can help me understand the logic at work here.0 -
Is there anyway to automatically find complete urls for 301 redirects?
Hello all, I'm working on a large site that is being prepared for a migration. After scraping on screaming frog, WMT, and deepcrawl, and deduping I have a list of 404 pages that need redirected. Many of these URLs are just incomplete URLs. For example the incomplete URL would be: "https://moz.com/beginners-guide-to" when it should be "https://moz.com/beginners-guide-to-seo" I have been finding the correct page manually searching google. With hundreds more to go, I am looking for a way the would be a little less laborious. Thanks! https://moz.com/beginners-guide-to-seo
Technical SEO | | AdamBohr0 -
.htaccess redirects
I've done some research but can't find a good answer to this question. Here's my situation: Site redirects from example.com to www.example.com just fine. However, it doesn't work so well for internal pages. My site incorrectly redirects (non-www) example.com/page2 to www.example.com when it should instead go to www.example.com/page2 So I need a method to redirect non-www internal pages to www versions. Currently I have this in my .htaccess - do I need to modify the rules? RewriteEngine on
Technical SEO | | 3plains
RewriteCond %{HTTP_HOST} !^www.example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L] Thanks0 -
.EDU via a 301 Redirect?
I recently received a link to my website from an .edu. However, the way they configured it was they pointed the link to one of their internal pages and then made that page 301 to my website. Is there anyway to gain any link juice from that sort of link?
Technical SEO | | gundogs0 -
Need Help writing 301 redirects in .htaccess file
SEOmoz tool shows me 2 errors for duplicate content pages (www.abc.com and www.abc.com/index.html). I believe, the solution to this is writing 301 redirects I need two 301 redirects 1. abc.com to www.abc.com 2. /index.html to / (which is www.abc.com/index.html to www.abc.com) The code that I currently have is ................................................... RewriteEngine On
Technical SEO | | WebsiteEditor
RewriteCond %{HTTP_HOST} ^abc.com
RewriteRule (.*) http://www.abc.com/$1 [R=301,L] Redirect 301 http://www.abc.com/index.html http://www.abc.com ...................................................... but this does not redirect /index.html to abc.com. What is wrong here? Please help.0 -
How long should I keep 301 redirects?
I have modified a the URL structure of a whole section of a website and used mod_rewrite 301 redirect to match the new structure. Now that was around 3 months ago and I was wondering how long should I keep this redirect for? As it is a new website I am quite sure that there are no links around with the old URL structure but still I can see the google bot trying from time to time to access the old URL structure. Shouldn't the google bot learn from this 301 redirect and not go anymore for the old URL?
Technical SEO | | socialtowards0 -
Worth Changing Redirect From 302 to 301?
Hi, I'm doing an audit on a site that had a redesign in Dec 2009. For some reason I looked to see what kind of redirects were used from the old pages to the current ones, and it looks like they used 302s, which obviously isn't ideal. Given that it's been so long and those pages have looong since been de-indexed, is it worth me suggesting that they change those old redirects to 301s? My thinking is that if those old pages were linked to externally then I should recommend it, but I can't find any link info on Linkscape/OSE, Majestic SEO or YSE. Any comments appreciated.
Technical SEO | | The_Heavies0