A script to automatically write 301 redirect rules to htaccess?
-
I was wondering if anyone could help provide some resources on how to automatically write 301 redirect rules to htaccess. Allow me to explain...
I'm building a new website and the primary users are businesses. They have their own profile pages on the site.
The URL is based off of their Company Name.
In the event that they decided to change their name... reasons being, perhaps they mispelled it the first time, or they're removing LLC or adding Inc, I want to also change the URL and redirect the old URL to the new URL.
Since the URL is based off of their Company Name, making a change to the company name would make a change to the URL. I know it doesn't have to work this way, but for our purpose this works best.
In case the old URL had any links to it, I wanted to see if there was an way to automatically update an htaccess file with a 301 redirect from the old URL to the new one.
Could anyone point me in the right direction of how to do this? Perhaps a sample script. I've done a lot of searches on Google and can't seem to find anything.
e.g.
Original:
Name: XYZ Widgets
URL: website.com/xyz-widgetsNew - business changes their company name in their profile:
Name: XYZ Widgets, Inc.
URL: website.com/xyz-widgets-incUpon the user saving the changes in their profile, I'd like to write a 301 redirect to an htaccess file:
Redirect 301 /xyz-widgets http://www.website.com/xyz-widgets-incI know how to manually write redirects and I've got a pretty smart web developer. We've just never triggered a script to automatically write to an htaccess file before.
Is this possible? Any resources are appreciated. Any security risks?
Thanks!
-
The rewrite done by WP is a 301.
-
Thanks for the reply. I've seen WordPress and Joomla do this before. But we don't use a CMS. It's a custom PHP framework. Although in my research I read that WP does a rewrite but not a 301 redirect. Not sure. WP has so many plugins so maybe there is one for redirecting changed file names. I'm sure between htaccess and a DB table we can do it. We'll definitely give it a try. I was just hoping to find an example of this online that I could view first.
-
So, what CMS are you using ?
I was in the same situation with one site. Luckily I was using Wordpress/Custom Posts. A change to a slug automatically creates a 301 redirect from the old url to the new url. There's even plugins that extend to allow the creation of the redirect even when changing the title. The 301 redirects don't actually go into htaccess, but they are handled by Wordpress and stored in the database. Another reason why if wordpress were a woman, I'd want to have babies with her.
Do some searching and see if there's a similar extension/plugin or something you can do to have that functionality within your CMS. If its custom, get your programmer to code it.
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
-
Reusing an already 301 redirected URL for a very important keyword
I have a question about reusing an already 301 redirected URL Till now I never reused an URLs that has been already redirected with a 301 redirect. However, I just started working on a website where in past they created a lot of 301 redirects without thinking about the future, and now certain URLs, that are currently redirected with a 301, would be very useful (exact match) and needed (for some of the most important keywords for this specific business), to maintain an optimal, homogeneous and "beautiful" URL structure. Has any of you ever reused a URL that was previously redirected with a 301 redirect? If yes what are your experiences with it? Can content on the reused URL (that was previously 301 redirected and than the redirect removed) normally rank if the page is reestablished and the redirect is removed (and you do great content, on page, internal linking, backlinking, .... ) or is such an URL risky / not recommended / "burned" forever and not recommended to be reused again... especially for very important keywords since it present the exact match ?! Thank you very much for all your help! Regards
Technical SEO | | moz46y0 -
How to Choose destination page for a 301 redirect?
I am doing some SEO for a wedding chapel in Vegas. There are some old packages that no longer exist and the bounce rate for the page is high so I am planning to 301 the page. How to best determine the best 301 destination? I have a few options. As an example the page was optimized for garden weddings. The page itself does not place well in the SERPS for garden weddings in Las Vegas, but our outdoor wedding packages in Las Vegas page places in the top 10. So that page is in an option. However, there is a different location that has a garden setting. Is that a better choice? Some content might match better than others, but any page I choose would be relevant content. Thank you so much 🙂
Technical SEO | | leslieevarts0 -
Blogger to Wordpress 301 and Meta Refresher Redirect
Hi Everyone! So my client has a blogger that she has developed a good amount of link equity for. It is a hersite.blogspot.com (she doesn't own her own domain yet). She is moving to the Wordpress platform though and the only way we can do a redirect is through a meta refresh redirect (since she doesn't have access to the servers on blogger). I went to Google Webmasters to do a change of address and found that the 301 checker said it couldn't find any 301 redirect, which is disappointing. What we're planning is telling all the places that link to the blog to change their links to the new blog but other than that what does anyone recommend to keep this link strength? Thanks!
Technical SEO | | mattdinbrooklyn0 -
301 Redirecting weird URLs with % in them
I've been working on redirecting links reported as 404 in Google webmaster tools. I've stumbled upon 41 URLs that Google is reporting as 404 that include a '%' in the URL, but I don't know how to redirect. Here is an example: URL: bond_information.htm%20Surety%20Bond%20Information,%20with%20FAQ Attempted redirect: redirect 301 /bond_information.htm%20Surety%20Bond%20Information,%20with%20FAQ http://www.mysite.com/ Unfortunately, after implementing the redirect, http://www.mysite.com/bond_information.htm%20Surety%20Bond%20Information,%20with%20FAQ still resolves a 404 error. Anyone successfully fix these errors using Apache .htaccess?
Technical SEO | | TheDude0 -
Loss of search engine positions after 301 redirect - what went wrong?!?
Hi Guys After adhering to the On Page optimisation suggestions given by SEOmoz, we redirected some of old urls to new ones. We set 301 redirects from the old pages to new on a page by page basis but our search engine ranking subsequently fell off the radar and lost PR. We confirmed redirection with fiddler and it shows 301 permanent redirect on every page as expected. To manage redirection using a common code logic we executed following: In Http module, using “rewrite path” we route “all old page requests” to a page called “redirect.aspx? oldpagename =[oldpagename]”. This happens at server side. In redirect.aspx we are redirecting from old page to new page using 301 permanent redirect. In the browser, when old page is requested, it will 301 redirect to new page. In hope we and others can learn from our mistakes - what did we do wrong ?!? Thanks in advance. Dave - www.paysubsonline.com
Technical SEO | | Evo0 -
Do search engines treat 307 redirects differently from 302 redirects?
We will need to send our users to an alternate version of our homepage for a few hours for a certain event. The SEO task at hand is to minimize the chance of the special homepage getting crawled and cached in the search engines in place of our normal homepage. (This has happened in the past so the concern is not imaginary.) Among other options, 302 and 307 redirects are being discussed. IE, redirecting www.domain.com to www.domain.com/specialpage. Having used 302s and 301s in the past, I am well aware of how search engines treat them. A 302 effectively says "Hey, Google! Please get rid of the old content on www.domain.com and replace it with the content on /specialpage!" Which is exactly what we don't want. My question is: do the search engines handle 307s any differently? I am hearing that the 307 does NOT result in the content of the second page being cached with the first URL. But I don't see that in the definition below (from w3.org). Then again, why differentiate it from the 302? 307 Temporary Redirect The requested resource resides temporarily under a different URI. Since the redirection MAY be altered on occasion, the client SHOULD continue to use the Request-URI for future requests. This response is only cacheable if indicated by a Cache-Control or Expires header field. The temporary URI SHOULD be given by the Location field in the response. Unless the request method was HEAD, the entity of the response SHOULD contain a short hypertext note with a hyperlink to the new URI(s) , since many pre-HTTP/1.1 user agents do not understand the 307 status. Therefore, the note SHOULD contain the information necessary for a user to repeat the original request on the new URI. If the 307 status code is received in response to a request other than GET or HEAD, the user agent MUST NOT automatically redirect the request unless it can be confirmed by the user, since this might change the conditions under which the request was issued.
Technical SEO | | CarsProduction0 -
Multiple Domains, Same IP address, redirecting to preferred domain (301) -site is still indexed under wrong domains
Due to acquisitions over time and the merging of many microsites into one major site, we currently have 20+ TLD's pointing to the same IP address as our "preferred domain:" for our consolidated website http://goo.gl/gH33w. They are all set up as 301 redirects on apache - including both the www and non www versions. When we launched this consolidated website, (April 2010) we accidentally left the settings of our site open to accept any of our domains on the same IP. This was later fixed but unfortunately Google indexed our site under multiple of these URL's (ignoring the redirects) using the same content from our main website but swapping out the domain. We added some additional redirects on apache to redirect these individual pages pages indexed under the wrong domain to the same page under our main domain http://goo.gl/gH33w. This seemed to help resolve the issue and moved hundreds of pages off the index. However, in December of 2010 we made significant changes in our external dns for our ip addresses and now since December, we see pages indexed under these redirecting domains on the rise again. If you do a search query of : site:laboratoryid.com you will see a few hundred examples of pages indexed under the wrong domain. When you click on the link, it does redirect to the same page but under the preferred domain. So the redirect is working and has been confirmed as 301. But for some reason Google continues to crawl our site and index under this incorrect domains. Why is this? Is there a setting we are missing? These domain level and page level redirects should be decreasing the pages being indexed under the wrong domain but it appears it is doing the reverse. All of these old domains currently point to our production IP address where are preferred domain is also pointing. Could this be the issue? None of the pages indexed today are from the old version of these sites. They only seem to be the new content from the new site but not under the preferred domain. Any insight would be much appreciated because we have tried many things without success to get this resolved.
Technical SEO | | sboelter0 -
301 redirect while keeping OLD domain for branding
Say you have CharityName.com. They use a dedicated domain name CharityNameEvent.com to advertise their main event. They use this domain on posters, flyers,etc and want to keep using it because it's easier to remember. CharityNameEvent.com has far, far more inbound links than CharityName.com (about 8 times more). Current problem: their current web developer has put the SAME content on both websites instead of setting up a redirect from CharityNameEvent.com (easy to remember) to CharityName.com/Event which would have made more sense. My intention is to consolidate the 2 websites and make sure CharityName.com benefits from links to the Event. I plan to move and 301 redirect CharityNameEvent.com to CharityName.com/Event. I know this would keep links and PR intact but I have a couple of questions: 1. Is it enough to set up the 301 redirect or would they have to ask websites to ACTUALLY change the links to CharityName.com/Event? 2. They plan and need to keep using CharityNameEvent.com for its ease of use on posters, flyers, etc. The 301 redirect would be in place. Would this cause any problems with search engines, especially when/if some people STILL link to CharityNameEvent.com instead of CharityName.com/Event? Basically, my understanding of 301 redirects is that they're used when a website permanently moves. In this case, the OLD DOMAIN name would still be used for reasons mentioned above but would be 301 redirected to CharityName.com/Event. Any chance this might not maximise the potential of new/old links? Any other way to go about it? Anything I'm missing with this scenario? Thanks
Technical SEO | | carmenmardiros0