Urls rewriting "how to" with .htaccess
-
hi,
Please i would need advices (links, tips, tool:generator ?) regarding url rewriting through .htaccess (newbee about it).
It's a "refurbishing" website case , the domain doesn't change. But the CMS does !
I've got a list of urls (800) with which i don't want to loose rankings on :
Here the type of old url syntax :
http://www.mydomain.com/home/newscontent.asp?id=1133
Here the new url type would be:
http://www.mydomain.com/name-of-the-article
or/and
http://www.mydomain.com/category/Page-2Tks a lot...
-
You should get all the url of the old site with Xenu's Link Sleuth, then create a PHP array of oldUrl => newUrl and put it in your redirect script.
So you have in the htaccess :
RewriteCond %{REQUEST_URI} ^/home/newscontent.asp
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^(.*)$ redirect.php?id=%1 [L]In the redirect.php file, you have :
$redirect = array("/home/newscontent.asp?id=1133" => "/name-of-the-article"); // 800 times (for all url)
if(isset($redirect[$_SERVER['REQUEST_URI']])) {
header("Status: 301 Moved Permanently", false, 301);
header("Location: http://www.mydomain.com/".$redirect[$_SERVER['REQUEST_URI']]);
exit();
}// Send a 404 if you don't have a redirect
-
Hi, i was thinking of the whole picture of baptiste solution, you say :
"Baptiste: On the new linux hosting set up an .htaccess file in the root of the site directory that redirects all id=xxxx requests to a redirect.php file on your server. The redirect.php file will need to interrogate a database with a table of the mappings and automatically redirect to the correct page via php scripting."
it means that wiithout any credentials, any database access, if you have urls from the site you need to move to, you can redirect any urls site to another one !?
Hum..i think i miss something ..
-
Good idea..i'll to make it so , and use excel function.....tks
-
Many tks for all these explanations..
So, in fact, lazily speaking, i would say that the .htaccess file solution give less work to do (no redirection script) and seems to be quite easy to make (excepting syntax inside .htaccess), so i 'll go for Damien's ..but i need credentials to install it.
Otherwise, if i don't, I'd go for Baptiste's...
Tks a lot...
-
As you have only 800 urls, I agree with Damien, you should generate an associative array in pure php, associating every ID with the new url.
The redirect script will only test if the ID is an array key, if it is you 301 to the new url. Otherwise, display a 404 page.
-
OK in that case it simplifies things a bit.
In order to do any redirection from id=1136 to unique-article-name you will haveto create the mappings entirely manually.
The two solutions provided are:
Baptiste: On the new linux hosting set up an .htaccess file in the root of the site directory that redirects all id=xxxx requests to a redirect.php file on your server. The redirect.php file will need to interrogate a database with a table of the mappings and automatically redirect to the correct page via php scripting.Mine: essentially the same as Baptiste's proposal, except that you don't interrogate the database, all the redirections are done using the htaccess file which contains all the mappings.
Either way you will need to manually create the mappings yourself, either in the database or in the htaccess file.
EDIT: Just had a thought, are the page titles of the articles the same between the new site and the old? If they are then you could crawl both sites with Xenu and then use vlookups in excel (or similar) to semi-automatically create your mapping of id = unique-article-name.
-
I'd say yes for the first one and for sure no for the second one...:)
-
To be honest, this is the solution I'd go for.
Mozollo, was your old site database driven?
Are you using the old article titles as the new page names?
If the answer is no to either of these, then the end result is you will have to manual map id to page name for each of the 800 pages you want to keep.
-
Tks again, so (sorry to repeat)
-
your solution : 1 .htaccess + redirect.php : located at the root of windows platform
-
Damien's : 1 .htaccess :located at the root of windows platform
Is that correct ?
-
-
1. .htaccess won't exist on the windows platform unless you installed a rewrite mod on the windows server. If you did then the .htaccesswill be in the root folder of the website (usually) you should check the documentation of the rewrite mod to confirm that.
2. If you have a windows PC then Xenu's Link Sleuth should be able to crawl the old site, you can then extract the information from the files that xenu can export.
3/4. If every unique id needs to get mapped to a unique url then yes, 800 times it is. If you have multiple ids that go to the same page you could do:
RewriteCond %{QUERY_STRING} ^id=113[3-8]$ [NC]
RewriteRule ^newscontent.asp$ ^name-of-the-article$ [L,R=301]
All ids from 1133 to 1138 will now redirect to the same page, you'll have to work out the regexs though.
-
To be clear about the different roles of the files in my solution, the .htaccess file will redirect every old url (whatever the id is) to a redirect script written in php.
This script will get the old url Id, load the article (to get the article name) and then redirect 301 to the new url. Only in php can you access the database.
Damien gave another solution, only based on htaccess. You have to write (or generate with code / software) 800 redirect directive for the htaccess file.
-
Tks to you both Baptiste placé Damiens Phillips and.
What do you mean when you say :
"The redirect.php file will load the article (or category as I understood) and do a 301 to the new url."
Is it en .htaccess file to create or a dedicated file.php , or both (redirect.php) ?
Yes, i'll all have to transfer each old article and i'll give them an unique urls per article..hope that reply your question !
-
Can you be a bit more precise about the new url ? Does every old article with id has to 301 to a page with a unique name ?
-
Hi,
Tks to you both Damiens Phillips and Baptiste placé.
But it seems to be a bit confusing for me for 2 reasons : language + technical knowledge !
I confirm that i'll move from windows platform to linux one.So if i understand :
1/ - htaccess is possible but where will it be located ? I assume at the root of the old platform (windows here..).
2/ - I'll have to crawl each article in order to get each id (by the way, have you got any crawler tool to advise ?)
3/ - For each of these urls i'll have to write such syntax :
RewriteCond %{QUERY_STRING} ^id=1133$ [NC]
RewriteRule ^newscontent.asp$ ^name-of-the-article$ [L,R=301]4/ ...800 times ? Or is there a way to tell on 1 line like :
RewriteCond %{QUERY_STRING} ^id=1133$ + ^id=1134$ + ^id=1197$ ...... [NC]Tks a lot again
-
I'll return the favour if it turns out he has moved from IIS
-
That's right but htaccess was asked. Thumbed up your answer so it goes first
-
But only if he's moved from Windows IIS hosting to Linux or Windows + PHP!
-
True ! The good syntax is :
RewriteCond %{REQUEST_URI} ^/home/newscontent.asp
RewriteCond %{QUERY_STRING} id=([0-9]+)
RewriteRule ^(.*)$ redirect.php?id=%1 [L] -
He'll need to add [L,R=301] at the end instead of just [L]. IIRC default behaviour is a 302 redirect.
You also can't reference a querystring in the RewriteRule, you have to use RewriteCond.
-
Hi,
From the .asp in the sample URLs I'm guessing you're hosted on Windows, if that's the case you'll need to get a rewrite mod for IIS such as ISAPI Rewrite 3. We've been using it for about 5 years now and it performs well. Their site has documentation that shows how it works.
You'll need to learn about regex expressions and a tool like Regex Buddy might be helpful.
I'm not aware of an tools that can automate generation, and I think that in your case you're going to need to do some manual work to set it up.
First you'll need a way of linking the old URLs to the new ones. Given the information you've provided, it's not clear how you'll be able to do this, so I'll make an assumption.
Assuming that name-of-the-article is the same as the title of newscontent.asp?id=1133, you'll need to generate a list, in excel for example, that lists the old contentid and the title of that document. You can then use formulae/macros to generate the rewrite rules which you would enter in the .htaccess file.
If you don't have a record of the id = title relationship in your old cms database (assumption!) then you might be able to do it by crawling the old site with a crawling program, exporting the data and then manipulating it. Otherwise you'll have to do it all by hand.
Rewrite rules generally take the form:
RewriteRule oldpageaddress newpageaddress [flags]
You'll also need to use the RewriteCond in order to base the rule on the querystring.
So for your example;
RewriteCond %{QUERY_STRING} ^id=1133$ [NC]
RewriteRule ^newscontent.asp$ ^name-of-the-article$ [L,R=301]
You'd then need to repeat those two statements for each page you want to redirect.
-
Hi mozllo,
You won't be able to create a .htaccess for such urls, because the original url only has the ID of the article and you want the name of the article in the new url. This requires database access to know the new url.
I would suggest to put in your htaccess file :
RewriteRule ^home/newscotnent.asp?id=([0-9]+) redirect.php?id=$1 [L]
Edit : see good rule below
The redirect.php file will load the article (or category as I understood) and do a 301 to the new url.
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
-
What Should be the URL of third party blog for my website
Hi All, What Should be the URL of third party blog for my website. Lets say if i have a website xyz.com (which provide pest control services in New York City) and I want to create third party blogs on platforms like blogspot, weebly etc so should i create the blog url as xyz.blogspot.com or i can use a keyword in the blog URL such as pestcontrolnyc.blogspot.com? Which URL would be the best as per Google's guidelines. Note: I have already have a blog within the domain as xyz.com/blog/ and i do weekly posting on this blog. But at the same time i want to create 1-2 third party blogs to get some backlinks from other sources.
Link Building | | Mr.Suren0 -
GWT Lists URLs with links to my site, but those URLs don't have links to my site
There are a number of URLs that show up for my website in Webmaster Tools as having links to my site when in fact they don't. I know this to be a fact because I actually wrote some of the sites that Google is saying are linking to my site and I know there isn't any links on the specific pages to that site. For example, the site in question is: https://www.liftproducts.com/ As an example, Google is saying that about 100 URLs from https://www.lift-tables.net/ are linking to it when, I'm fairly confident that there is only 1 link to it found here: https://www.lift-tables.net/info.php?countrytabs=3 Yet Google says links exist on this (and many more) page: https://www.lift-tables.net/liftproducts/max-lift/lpt4w-030-48.php I thought at first it might have been due to linking to a image file directly instead of locally hosting it, but after checking the source, that's not the case. I am also seeing this type of non-linking being reported on sites I have no clue what they are. For example, some Spanish site about tandem bicycles is being listed as linking to liftproducts.com: http://206-225-85-34.dedicated.abac.net/tandem/inc Yet I can find no evidence of an actual link existing. Am I missing something here? Any insight of why this is happening and how I can clean up my link signature would be appreciated.
Link Building | | Nivik231 -
Are sites that "smell of SEO" being demoted?
I'm working with a site owner who recently hired an SEO to work on just one particular type of keyword. It seems like the more work the SEO did, the lower the keyword gradually dropped. Granted, the "work" is pretty low quality - anchor texted bookmarks, comments and low quality articles. We're doing an experiment where we are going to disavow those links and see if the previous rankings return. Another site that I am consulting with has a lot of good natural links and then some anchor texted links, but from decent sources - some guest posts (on good sites - not a spammy site that exists only for guest posts) and some places where decent websites have agreed to link to the site. The anchor texted links do not make up very much of the overall anchor text. There is good diversity and lots of brand and url anchored links. It seems like the more the SEO does for this site, the more the rankings drop. And it's not all about anchor text. The SEO placed a link in a relevant directory, using the url as anchor...rankings dropped a little. They obtained an expired domain with relevant and very natural links and 301'd it to the new domain...rankings dropped several places. And so on. Occasionally the rankings will pop up a little, but overall it's a downward spiral. Check out this post by Gary Taylor. He did an experiment where he took an established site that was ranking well and threw some spammy links at it. To quote the article, 'every day goes by my ranking for the term “domains” is getting harder to maintain.' He recently tweeted that he has been removing and disavowing links and the rankings are returning. I was looking at searches for real estate related terms in different cities. In some cities, the top sites are ones that have ZERO obvious SEO done to them. There are sites ranking on page 3 that have been SEO'd, and not all of them have poor SEO. Many are what I would consider really well done. Some of the sites ranking on page 1 have under 10 links. There was one with 2 followed links and they were not from super authoritative sites! To complicate matters though, if you look at searches like "Toronto Real Estate Agents" some of the top sites have lots of keyword anchor texted links. Perhaps this should be a blog post rather than a Q&A, but I would love to hear some of your thoughts. My personal thought is that Google's main goal with Penguin and the unnatural links warnings is to make it so that not only is it not profitable to try to manipulate the SERPS, but that every time you try to do so, you potentially do your rankings harm. I used to say that Penguin could only affect a site on the date of a Penguin refresh, but I am thinking now that Google has managed to roll Penguin into the algorithm to some extent so that it can demote the majority of any work that smells of SEO. Thoughts?
Link Building | | MarieHaynes0 -
Question on getting new site with poor URLs indexed
For the website www.misslovegirls.com , with URLs like http://www.misslovegirls.com/goods.php?id=3706 is there any chance of getting these URL's indexed? Thanks!
Link Building | | onlinemktg10 -
A link with "return false"- OSE sees as a No Followed Link
Hello, I couldn't find a clear answer to the impact on SEO for a link written in this way: [" class="expert_info" onclick="window.open(this.href);return false;">](w</span>ww.yourwebsite.com<span style=) [Does the "return false" act as a "no follow"? I came across this in our link data in Open Site Explorer which lists these links all as "no follows." However, an engineer I spoke to said that it shouldn't impact search engine behavior. Any ideas? Thank you in advance! -Sarah K.](w</span>ww.yourwebsite.com<span style=)
Link Building | | OneMedical0 -
Same Page, Multiple URLS- Canonical tag? Redirect?
I just read this article about how google views multiple versions of the same page as different pages of duplicate content. My website, www.telikin.com currently has many versions of the home page. There is: www.telikin.com
Link Building | | Telikin
www.telikin.com/index
www.telikin.com/index.php
www.telikin.com/index.php/ All of these seem to be their own URLS, If i use the open site explorer tool to search for links pointing to these URLS, the www.telikin.com domain shows about 700 links, and the www.telikin.com/index.php shows about 63 links. My questions: Is my SEO suffering as a result of having these different pages? Does google realize that links pointing to www.telikin.com/index.php should count towards the seo of the www.telikin.com page? If this is hurting my company's website, how do I solve this problem? I am not versed as a webmaster, so I don't understand what the best course of action would be. Should I request that our webmaster somehow redirects all traffic from the other URLS to the main www.telikin.com URL? Should I be using canonical tags on all important pages? If i do, for example, use a canonical tag on the www.telikin.com page, will Google then know to send all link juice from the www.telikin.com/index.php page to the www.telikin.com page? We also have a Joomla blog that seems to duplicate pages many times over, but that is a problem that I will have to address another time. Any comments/suggestions are more than welcome. Thanks, Dave0 -
Should I use a branded url or an exact match, and which branded url?
I'm thinking of changing my client's 37 character exact match url because it looks spammy to me and Google says it will be turning the dial down on exact match domains. I still see a lot of value in it however and it is helping this client, albeit very little since he is new in his arena. His goal is to rank for keywords in that url so I hate to lose the benefit of that url. As an example, let's say his url is floridahealthinsurance-quote.com I'm concerned now because I plan to do some reputable and highly relevant link building and I'm afraid bloggers might hesitate linking to a spammy sounding url. This client does have a branded url that is short and sweet, something like millerbenefits.net, and that redirects to the long url. Back linkers could link to the short url instead. However, it lands people on the site with the long spammy-sounding url and they may not want to be affiliated with it. Something like millerbenefitsinsurance.com is available and it does have 2 keywords in it. Should I get that instead? In short the question is this:
Link Building | | KatMouse
Should I change or keep floridahealthinsurance-quote.com If I should change it, should I change it to:
millerbenefits.net
millerbenefitsinsurance.com Thanks for your help!0 -
Is there a cost per acuqistion figure I can quote that if I hire an agency to link build that we can guarantee a level of increased traffic from Google?
I am looking to justify to a pubilsher the cost outlay to hire an agency to do white hat link building for me as we do not have time. (the site is fully optimised but we lack the inbound links our competitors have) - is there a way I can explain in terms of numbers ie page impressions that if we build for eg 10k high quality anchor text links into our site we will get x thousand more page impressions as our rankings in the search engines will increase? Just need a justification before the budget is allocated.
Link Building | | Pday0