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
-
Should backlink URLs include www, https, or both???
My business is located at remotebaba.com, and I am wanting several sites to link to it.
Link Building | | udaipabla
I am only interested in home page at this point, and we are using HTTPS for our site. Which one of these should I have other sites link to (Please provide complete justification):
remotebaba.com (my root domain)
www.remotebaba.com
https://remotebaba.com
https://www.remotebaba.com I really appreciate your help!1 -
What are inbound links worth after Panda and Penguin, since a site ranks for the NATIONAL keyword "outdoor furniture" with ONLY 7 INBOUND LINKS!?!?
ARE INBOUND LINKS TODAY ALMOST WORTHLESS??!!? After having been the KING of SEO, down to the toilet? Today I got a great sample from a fellow SEO professional showing a site, rockymountainfurniture.com rank for "outdoor furniture" with ONLY 7 INBOUND LINKS FROM 3 DOMAINS and a domain trust of 14!!!! ON A NATIONAL, VALUABLE, HIGH QUERY VOLUME KEYWORD!! I was stunned, but is this the "NEW SEO WORLD" we live in, so we should skip spending hundreds of hours on link building? There are over 32 different types of inbound links but this is much MORE than a radical change, this is turning SEO as it was up-side-down. Any input, thoughts, ideas?
Link Building | | yvonneq0 -
Bought links from sites with target keyword in their URL
One of my rivals in a competitive field (real estate) has done this deliberately and extensively -- it accounts for about 2/3 of his inbound links. Result: he's now #1. How strong a ranking factor is keywords in source URL - ie, how easy is it to beat with other ranking factors?
Link Building | | Jeepster0 -
Are moderated external links in a "showcase" worth having?
We have some e-commerce software with several dozen websites that use our technology. The websites have links to the developer and design shops that made the site as well as the site itself. All of these sites are moderated so we don't have to worry about spam. My question is should some or all of these links be rel=nofollow? The links are definitely pointing to legit development shops but those sites aren't always strictly related to e-commerce or the keywords we're trying to rank for. Any thoughts on how to treat these links?
Link Building | | schof0 -
Footer Back Links Question "Site by built by"
Hi all I design and develop websites for clients within the UK. With each website I build, I always add a link to the footer of the website which links back to my website. I have always used the link anchor text "Site By Jump" but then decided to try to use the link anchor text to our benefit by changing the links across all of our portfolio of sites to be: "Graphic Design by Jump"
Link Building | | yousayjump
or
"Web Design by Jump" In the hope that this helped us rank better for those keyword phrases. Now something occurred to me the day. Most of the websites I build have no relevance to the content on our website. For example, I could build a website for Baby Food and add a backlink to our homepage which doesn't even mention the words "baby food". In some cases, the websites can have thousands of pages, each with this footer link appearing at the bottom of each page. My question is, could these backlinks potentially be seen as black hat or spam to a search engine? I.e. thousands of backlinks from websites that have no relevance in terms of content all linking back to my homepage? Thank you for reading and any advice would be greatly appreciated.0 -
Searching for Quality "Follow" Back Links
I'm in a highly competitive national market where the top sites have links from between 325 and 1300 unique linking root domains, therefore, you have to have an aggressive approach just to get on the map. (I'm at 317) If we were talking about needing 50 good links, I could take the time to cultivate relationships, get to know people, and get 1 or 2 great links from each webmaster, but the scale of the challenge is out of control. My competitors, and myself, seem to all be getting links in the following ways: Hoards of directory links. Some high quality paid links from industry sites ($2,400 per each link per year) and hundreds from 9-$49 per year. At the bottom of the list of most all my competitors, there appears to be some links from their early beginnings that were reciprocal linking arrangements. Blogs where they submitted articles and have good links back to their sites. Paid ads on sites all over the internet that link back with their specific key words. Some from relevant sites, but mostly from sites that would give them a good deal and have high enough traffic and/or page rank. Blog comments with a link back to their site; sometimes with good anchor text and sometimes you're forced to have to use your web site address as the anchor text or even your name. (Does that even do any good?) My dilema is where to find 1,000 good places to get links and I don't do black hat? I can write good quality comments on blogs from a wide variety of industries, but most are now eliminating the possibility of using my anchor text other than my web site and my name. As I scour the playing field, it almost appears that it has become a "pay to play" proposition as far as getting links everywhere other than writing good blog articles, but then what good does it do to have 500 blog articles coming from a handful of linking root domains? You're just stuffing the ballot box! As for me, I'm in the teens with all the high value phrases I need and must come up with a better strategy for the home stretch. In all the other varied statistical measurements that I see on SEO Moz, I'm no lower than #5 out of the top 10 competitors in any of them except Alexa rank. So, I'm close but it seems so far away! Would appreciative and be grateful for some wisdom from the community! Lowell
Link Building | | lwnickens1 -
How do I get Bing to recognize my site as a "News" source?
We are a publisher printing a magazine with corresponding news on our website and would like to be included (obviously) into the Bing news area, any tips on getting Bing to notice and include us?
Link Building | | KenPiech0 -
Are In bound links to a Custom URL ineffective
Hi All, I would like to know what would achieve the best inbound link ranking from google. I have to run an inbound link campaign and have to choose between two options: Option 1, get people to link directly to the home page or Option 2, get people to link to a custom url (for example, www.domain.com/index.aspx?dealer1455) the custom url has a function to hide certain menu items on the home page. Also The custom url ID number will also change for each person that links to us. So in effect each link is directed to an individual custom URL. My preference is option 1. But a number of the people linking to us would prefer to link to a custom home page that has their competitors details hidden. At the end of the day the decision needs to be made on what will provide our site with the best rankings in the serps What option is best to improve search engine rankings ? Many Thanks in advance
Link Building | | SaimanRhino0