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
-
Can a page that's 301 redirected get indexed / show in search results?
Hey folks, have searched around and haven't been able to find an answer to this question. I've got a client who has very different search results when including his middle initial. His bio page on his company's website has the slug /people/john-smith; I'm wondering if we set up a duplicate bio page with his middle initial (e.g. /people/john-b-smith) and then 301 redirect it to the existent bio page, whether the latter page would get indexed by google and show in search results for queries that use the middle initial (e.g. "john b smith"). I've already got the metadata based on the middle initial version but I know the slug is a ranking signal and since it's a direct match to one of his higher volume branded queries I thought it might help to get his bio page ranking more highly. Would that work or does the 301'd page effectively cease to exist in Google's eyes?
Technical SEO | | Greentarget0 -
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 -
Backlink management: 301 redirect unsuccessful.
I am managing my company's spammy backlinks using Open Site Explorer. Our company owns a few URLs that are related to our company or are iterations of our main URL. All of these additional URLs have 301 redirects to our main domain. Open Site Explorer has identified one of these URLs as having a spam score of 8 indicating a 56% chance of Google crawler penalization. Obviously, this is a red flag. Instead of being redirected to our main domain upon visiting the URL, I was directed to what seems to be an automatically generated, generic webpage with links that seem to have been generated by keywords from our main domain. I have seen this type of webpage before when incorrectly typing in URLs from other pages. They tend to look the same. They have a black background with the URL written in grey at the top and a rectangular related links bar. Is anyone familiar with my problem and could you offer any advice? Thanks, Ben
Technical SEO | | SOLVISTA0 -
301 redirects without .htaccess
I have a client that recently moved from an old ZenCart e-commerce site to Volusion. The domain name did not change. We need to redirect a bunch of the old URLs; however, Volusion's redirect tool does not work for URLS with "?" . The old ZenCart structure is: http://www.mydomain.com/index.php?main_page=product_info&products_id=563 Volusion is a hosted platform and we do not have access to the .htaccess file. Any suggestions on a workaround? Thanks!Nancy <colgroup><col width="960"></colgroup>
Technical SEO | | NancyVPS
| |0 -
301 redirect blog posts from old URL to new one
I moved a wordpress blog from domain.com to domain.com/blog . I want to redirect the links in google from the old domain.com to the new one, but I also want to put a new site/application at domain.com..so I'm thinking an .htaccess 301 redirect at the root wouldn't work. Any tips?
Technical SEO | | callmeed0 -
Automatic redirect to external urls
Hi all, I'm developing a dynamic qr code service.. The service works in the following way: You create an account with an associated QR CODE pointing to a url like:
Technical SEO | | raulo79
- http://domain.me/username The user can change the target of this url.. he can:
- point to an external url ( his website for example)
- point to a vCard download page
- a mobile ready webpage ( no redirection in this case)... Visiting http://domain.me/username my company logo is displayed and we redirect the visitor with a: header("Refresh: 5;URL=http://userdomain.tld"); Google is indexing many user's URLs, this is good for those users pointing to the mobile ready webpage, in this case there is no redirection, but Google is indexing many urls that redirect to an external url and I don't know how to avoid this.. I can't do an header('Location: http://www.example.com/'); because I need to display our logo after redirection.. how can I do google friendly? Sorry for my english, I hope you can undestand the problem. Best regards.
Mauro.0 -
301 redirects for individual products - should I keep the old ones?
Hello We are just now starting up a niche site that will be the new home for a large group of products from an old site. Apart from straight up informative text links, we have set up 301 redirects for the 100 most important products from the old to the new site. Right now, we are in a transition period where we openly tell our visitors that we have a new site for this certain group of products. My question is: for how long should we keep the products on the old site? Can we remove them straight away, since our intentions of the 301 redirects is to preserve the serp positions for the most important products? Does it matter to google if we let the products remain on the old site for a while? Regards
Technical SEO | | jsigwid
Oskar0 -
Switching ecommerce CMS's - Best Way to write URL 301's and sub pages?
Hey guys, What a headache i've been going through the last few days trying to make sure my upcoming move is near-perfect. Right now all my urls are written like this /page-name (all lowercase, exact, no forward slash at end). In the new CMS they will be written like this: /Page-Name/ (with the forward slash at the end). When I generate an XML sitemap in the new ecomm CMS internally it lists the category pages with a forward slash at the end, just like they show up through out the CMS. This seems sloppy to me, but I have no control over it. Is this OK for SEO? I'm worried my PR 4, well built ecommerce website is going to lose value to small (but potentially large) errors like this. If this is indeed not good practice, is there a resource about not using the forward slash at the end of URLS in sitemaps i can present to the community at the platform? They are usually real quick to make fixes if something is not up to standards. Thanks in advance, -First Time Ecommerce Platform Transition Guy
Technical SEO | | Hyrule0