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
-
Should I set 301 Redirects or keep old permalinks?
I just built a new site for a client and the new site has pretty links like domain.com/about-us. The old site has been around for a VERY long time and had links like domain.com/aboutus. I want to do 301 redirects because from my understanding, it minimizes the negative SEO impact. But the client wants to keep the permalinks without the hyphens to match the old links. What is the best route, considering SEO? BTW, the site is still on the same domain. Thanks in advance for your help! Neik
Technical SEO | | glassh0use0 -
301 Redirects, Sitemaps and Indexing - How to hide redirected urls from search engines?
We have several pages in our site like this one, http://www.spectralink.com/solutions, which redirect to deeper page, http://www.spectralink.com/solutions/work-smarter-not-harder. Both urls are listed in the sitemap and both pages are being indexed. Should we remove those redirecting pages from the site map? Should we prevent the redirecting url from being indexed? If so, what's the best way to do that?
Technical SEO | | HeroDesignStudio0 -
Redirects
Hello, i would like to ask which is the best redirect to use in a website now i have all my links to go to https://www.anavasis.gr Which is the best between: https://www.anavasis.gr https://anavasis.gr https://www.anavasis.gr/index.php https://anavasis.gr/index.php thanks in advance
Technical SEO | | anavasis0 -
301 redirect not working
Hi there! I have recently moved a domain that has been indexed by google and setup redirects so that it forwards to the new domain. It seems like the only redirect that actually is working is the canonical and main domain but every other page and or page nested within a folder are not working. Here is an example of some of the redirects. Am I doing this wrong? It seems to be going to the new domain but can't find the actual pages.... RewriteEngine On
Technical SEO | | twotd
RewriteBase /
RewriteCond %{HTTP_HOST} !agoodsweep.com$ [NC]
RewriteRule ^(.*)$ http://agoodsweep.com/$1 [L,R=301]
redirect 301 woodstoveservicerepair.html http://agoodsweep.com/woodstoveservicerepair/
redirect 301 /westchesterchimney.html http://agoodsweep.com/west-chester-chimney/ Thanks in advance for any help!!0 -
301 redirect from Blogger
Hello, I have a client with a Wordpress network of blogs, each blog is owned by a different blogger. Many of them were migrated time ago from Blogger. I have seen that the way used to redirect them is a meta refresh, so no authority is being passed. I cannot find any reliable way of making a 301 from Blogger, There are some plugins, but I'm afraid of using them. Any of you have experience with this situation please? I have even thought about placing a global rel canonical before the meta refresh, but I think that here the problem is the meta refresh itself.... Thank you in advance
Technical SEO | | Juandbbam0 -
How does this rank? - a page that is 301 redirected
How does a 301ed page rank in google? In google I searched for" ikea.ca" which is set up as a 301 redirect to www.ikea.com/ca/en and was surprised to see the url --> www.ikea.ca actually ranking. IKEA Canada <cite>ikea.ca/</cite>IKEA Featuring Scandinavian modern style furniture and accessories. Include storage options, lighting, decor products, kitchen appliances and beds. Bedroom - Kitchen - Living Room - IKEA North York
Technical SEO | | Morris770 -
Setting up a 301 redirect from expired webpages
Hi Guys, We have recently created a new website for one of our clients and replaced their old website on the same domain. One problem that we are having is that all of the old pages are indexed within Google (1000s) and are just getting sent to our custom 404 page. We are finding that there is an large bounce rate from this and also, I am worried from an SEO point of view that the site could lose rank positioning through the number of crawl errors that Google is getting. Want I want is to set up a 301 redirect from these pages to go to the 'our brands' page. The reason for this is that the majority of the old URLs linked to individual product pages, and one thing to note is that they are all .asp pages. Is there a way of setting up a rule in the htaccess file (or another way) to say that all webpages that end with the suffix of .asp will be 301 redirected to the our brands' page? (there is no .asp pages on the new site as it is all done in php). If so, I would love it if someone could post the code snippet. Thanks in advance guys and if you have any other ideas then be my guest to suggest 🙂 Matt.
Technical SEO | | MatthewBarby0 -
My home page 301 redirects - is this an SEO problem
When ever a browser calls my site canineconcepts.co.uk, it is automatically 301 redirected to canineconcepts.co.uk/en I am not sure if I should be concerned about this from an SEO perspective or not. Any thoughts?
Technical SEO | | CanineConcepts0