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
-
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 -
Quote + Link from Associated Press
So almost everyone here has talked about the marvel (for a small business like me, at least) that is HARO. Recently, I was quoted in a story that was written by the Associated Press, by responding to a HARO inquiry. (story resides on their site here in case you want to look). So looking at it with a critical mindset, is this a glorified press release at this point (the thing shows up virtually everywhere since after all, it was written by the AP). Should I be expecting any SEO help from this?
Link Building | | MarkAselstine0 -
Need referrals on expert to clean up "unnatural links" and submit reconsideration request
I just received a manual unnatural links penalty. I was shocked because other than maybe five links many years ago, I've not done anything to get links. The site is very powerful in it's own right and buying links is unnecessary. My first thought was a competitor has done the link spamming on my behalf, any my site is very profitable and the competitors are plentiful and ruthless (i.e. steal my content and use it) since we are #1 for our search term. I took a quick look at my links (via webmaster tools) just now and yes, in the last few months there are tons of links from very bad looking sites that are I've never heard of. It's obvious I've been purposefully hit by a competitor. (What's it called when they do this?) Does Google understand at all that this stuff goes on? Do they even care? On the eventual reconsideration request, if I say that I honestly didn't do those links, would they even believe me or even care? I have read several articles on what to do now. But this is so important to my income stream that I don't want to screw anything up trying to fix it, and I want to get it right the first time. Every day is very expensive to me. So, I'd like to get some recommendations on any experts that can handle this professionally and swiftly. Please let me know of anyone trustworthy that won't rip me off, and if you are such a professional yourself, please let me know. Thank you
Link Building | | bizzer0 -
Does google really care if anchor text is the "brand name"?
I have read a lot about how it is a good idea to build links with your "brand name" as the anchor text. I am somewhat skeptical about all this. I suspect that the only thing that you need to do is have a wide variety of anchor text, some no follow links, and some links without any anchor text at all or just your URL. IDoes anyone have an opinion on this? Also I am somewhat confused about what a "brand name" is anyway. Obviolusly it applies to words like "Apple Computers" or seomoz but can I "make" my own name, i.e. Paul W. Matthews, into a "brand Name" aulby putting it in the title of my home page and just using it a lot as anchor text? If yes, is this a good idea? thanks, Paul
Link Building | | diogenes0 -
Drop in "Links to your site" in Google Webmaster Tools
Last week I noticed a substantial drop in "Links to Your Site" in Google Webmaster Tools on several of my websites. Upon further investigation the reduced links were from our sister sites. It appears that Google has reduced the number of links they are counting from links in our headers and footers from these sites to our main websites. It is not affecting our rankings or traffic. Is Google doing some clean up with what they are showing in webmaster tools for links from related, owned sites? Any cause for concern?
Link Building | | tdawson090 -
How would one get on the Huff Po's "Around the Web" list?
On the huffington post they often have lists of links related to the article, near the bottom just before the comments. This section, when present, is called "Around the Web". See this as an example: http://www.huffingtonpost.com/linda-bacon-phd-ma-ma/health-at-every-size_b_1314339.html Do the article authors choose these links, or are they auto-generated somehow? I had a relatively small competitor appear here, so I'm wondering if there are any tricks to gett included. Thanks! Edited to add hyperlink
Link Building | | AdoptionHelp0 -
How to improve "Total Links (as shown in SEOMoz)"
I manage http://www.foodieBytes.com and responsible for the site's SEO. I wanted to understand how SEOMoz calculates internal links. I have been trying to improve FoodieBytes internal site structure to increase the internal links number but so far it has not moved. In the image below (Root Domain Matrix as shown on Open Site Explorer on SeoMoz), the first column is for FoodieBytes.com and the second column is for a competitor's site. External link numbers are very close for both sites but the internal links for competition is way high. The number of pages on FoodieBytes and the competitor in the attached image are comparable. I am looking at the "internal link" numbers as a way to improve the "total links" profile for FoodieBytes in hope of SEO improvement for the site. Thanks for input. tyxQo.png
Link Building | | foodiebytes0 -
"Diverse" Links from same IP address worth the work?
Hi folks, This is my first posting, so please be nice with me 🙂 I have completed an all-new website (www.buy-hosting.net) , however the domain is bought and already exists since 2 years, but has no inbound links though. Now I start building links. I have some clients of my web design business, I could bring in links to my new site on their sites from different domains, some established and years old. However, they all are hosted at the same IP address. Is it a good idea for link diversity to post links on their site to my new one? I remember a thing, that only 1 link will be counted, originating from the same IP address. Is this worth the work of asking clients, posting links, etc.? Or is it even dangerous to my new site? Also, any other ideas about how to start link building are welcome. Is there a comprahensive list, something like a "startes guide with practical ideas" to link building? Thank you and kind regards
Link Building | | ie4mac0