.htaccess tutorial

htaccess Elite


Creating SEO Friendly URL redirector

Search Engine Optimization using Htaccess

Creating SEO Friendly URL redirector

New postby produke » 20 Oct 2006 13:10

Many dynamic websites employ some sort of navigation achieved through the query string. For example:

Code: Select all
http://www.htaccesselite.com/htaccess/posting.php?title=Mod_rewrite&action=edit


Just as this is difficult to read by humans, search engines also have trouble reading them. True, they aren't as bad as they used to be and engines like Google (http://www.google.com) claim to read the query strings correctly. One part of the problem is that a query string items can be place in a different order. The page can still read and process the variables, but in essence the actual URL has changed.

The option is to place a .htaccess document in the root of the website, and re-direct any requests that are not actual files or directories to a file that will process the request. Below is the .htaccess document that re-directs all of those failed requests to an index.php file for handling. An added bonus of this is that you won't have to worry about any pesky 404 messages, your file will handle the navigation and you can output your own error message.

.htaccess document:
Code: Select all
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /index.php [L]



Ok, now you have the .htaccess file in operation, you can use and address like:

http://wiki.dreamhost.com/Mod_rewrite/edit/1/

It looks like a folder structure, reads much easier and also disguises how your page operates by not showing the query string variable names.

In the PHP page you could handle this navigation like so:

Code: Select all
<?php
$navString = $_SERVER['REQUEST_URI']; // Returns "/Mod_rewrite/edit/1/"
$parts = explode('/', $navString); // Break into an array
// Lets look at the array of items we have:
print_r($parts);
?>


This page will output something like:
Code: Select all
Array (
[0] = ,
[1] = 'Mod_rewrite',
[2] = 'edit',
[3] = '1'
)


You can use your imagination from there to create something that will navigate off the information you've extracted from the URI.

Note that rewrite rules in a .htaccess file behave differently from those in a server config. If you're used to writing rewrite rules at the server level, your habits will get you in trouble when working in a .htaccess file. In particular it is so far as I know *never* valid to begin a rewrite rule with a slash. A rule like

Code: Select all
RewriteRule ^/anything http://other.server.com/somewhere


will never match, because the incoming URL never has a leading slash in directory context. This is explained in detail in the Apache documentation, see the resource links.
User avatar
produke
 
Posts: 242
Joined: 25 Sep 2006 04:48

Re: Creating SEO Friendly URL redirector

New postby serpico » 17 Apr 2008 03:45

Hi there

I am working on a very simple bespoke php/MySQL CMS. URLs are currently created as per the htaccess code given below.

For example, currently a request for page13.html will serve index.php?id=13

Code: Select all
RewriteEngine on
RewriteCond %{REQUEST_URI} /page(.*)\.html$
RewriteRule page(.*)\.html$ /index.php?id=$1


I would prefer something more SEF that instead uses the page title field from the database as the filename instead of the content id.

For example, there needs to be a rule that tells the server that when someone requests page-title-content.php to serve index.php?=X WHERE X = the id of page-title-content

Does that make sense?

Any help or direction on where to start would be very useful. Thanks.
serpico
 
Posts: 1
Joined: 17 Apr 2008 03:33

Re: Creating SEO Friendly URL redirector

New postby htaccess » 10 May 2008 00:45

You can't do that unless you manually redirect each specific id to the title.. .htaccess cannot access databases to know that id=5 should be the-title-is-this
htaccess
 
Posts: 50
Joined: 28 Feb 2007 17:16

Re: Creating SEO Friendly URL redirector

New postby olidenia » 13 May 2008 00:07

I have a problem on a hosted server, I have a classifieds script with friendly URL, but doesn't work:

Code: Select all
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule .* - [env=REWRITE_ON:1]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [L]
</IfModule>


I talked to the support on my server and they suggested this:

Code: Select all
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.pinrastro.es
RewriteCond %{REQUEST_URI} !/index.php
RewriteRule ^/(.*)$ /home/www/pinrastro.es/www.pinrastro.es/index.php?q=$1 [L]
</IfModule>


But still not working, They have confirmed that mod_rewrite is active on Apache and there shouldn't be a problem.

Any help or ideas?
olidenia
 
Posts: 4
Joined: 13 May 2008 00:00

Re: Creating SEO Friendly URL redirector

New postby htaccess » 15 May 2008 05:15

Place this in the file /home/www/pinrastro.es/www.pinrastro.es/.htaccess
Code: Select all
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.pinrastro\.es [NC]
RewriteCond %{REQUEST_URI} !^/index\.php.* [NC]
RewriteRule ^(.*)$ /index.php?q=$1 [L]
</IfModule>
htaccess
 
Posts: 50
Joined: 28 Feb 2007 17:16

Re: Creating SEO Friendly URL redirector

New postby olidenia » 15 May 2008 09:37

I got my answer...

###################################
Hello,

Then mod_rewrite is enabled and the redirection should work.
We have tested it and it appears it doesn't handle the requests properly,
as explained in the article you provided. We cannot offer a solution to
this for the time being. We will keep checking to see if we can come up
with assistance to get the nice URL method to function.


Best Regards,
Dave
###################################
olidenia
 
Posts: 4
Joined: 13 May 2008 00:00

Re: Creating SEO Friendly URL redirector

New postby olidenia » 15 May 2008 09:56

This is the article:

Nice URL feature:
Noah's Classifieds supports using nice URLs. This means for example that the link of an ad details page can look like this:

http://your.classifieds.site/item/10


instead of the current solution:

http://your.classifieds.site/index.php?item/10


Besides that the former one is nicer, it is also said to be more search engine friendly.

So that the nice URL feature works the Apache module called mod_rewrite must be installed. It can't be find out for sure whether it is installed currently (Php is probably installed as a CGI binary). If mod_rewrite is already installed, you should create a file under the classifieds installation directory called .htacces and put the following text in it, in order to enable nice URLs:

Code: Select all
<IfModule mod_rewrite.c>
      RewriteEngine on
      RewriteRule .* - [env=REWRITE_ON:1]
      RewriteCond %{REQUEST_FILENAME} !-d
      RewriteCond %{REQUEST_FILENAME} !-f
      RewriteRule ^(.*)$ index.php?url=$1 [L]
</IfModule>



If after doing this, the nice url feature still doesn't work, you should also check the following in the Apache configuration file:

* .htaccess override for the classifieds webroot is enabled,
* FollowSymLinks for the classifieds webroot is enabled,
olidenia
 
Posts: 4
Joined: 13 May 2008 00:00

Re: Creating SEO Friendly URL redirector

New postby olidenia » 15 May 2008 17:23

I forgot to say, Thank you for your help!!!

The problem whas on the server side.
olidenia
 
Posts: 4
Joined: 13 May 2008 00:00

Re: Creating SEO Friendly URL redirector

New postby produke » 11 Jul 2008 04:56

olidenia wrote:I forgot to say, Thank you for your help!!!

The problem whas on the server side.


What did you figure out? nice article..
User avatar
produke
 
Posts: 242
Joined: 25 Sep 2006 04:48

Re: Creating SEO Friendly URL redirector

New postby olivia » 03 Dec 2008 03:02

Hi folks,

Thank you for your useful info,
I have a problem in my server. How can I access c panel through my website?

Thank you in advance
olivia
 
Posts: 7
Joined: 03 Dec 2008 02:49

Re: Creating SEO Friendly URL redirector

New postby bainiu » 14 Jul 2009 07:58

Thanks for the info.I am learning this now. thanks.




_________________________________________________________________________________
http://www.storeingame.com WOW Gold
bainiu
 
Posts: 1
Joined: 14 Jul 2009 07:54


Return to SEO