htaccess Elite

Hire htaccesselite professional

$25 non-refundable deposit for me to look at your request,
$175/hr if both parties agree to work request.

.htaccess tutorial


All times are UTC - 5 hours





Post new topic Reply to topic  [ 8 posts ] 
Author Message
 Post subject: Creating SEO Friendly URL redirector
PostPosted: Oct 20th, '06, 07:10 
User avatar

Joined: Sep 24th, '06, 22:48
Posts: 240
Many dynamic websites employ some sort of navigation achieved through the query string. For example:

Code:
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:
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:
<?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:
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:
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.


Top
 Profile  
 
 Post subject: Re: Creating SEO Friendly URL redirector
PostPosted: Apr 16th, '08, 21:45 

Joined: Apr 16th, '08, 21:33
Posts: 1
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:
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.


Top
 Profile  
 
 Post subject: Re: Creating SEO Friendly URL redirector
PostPosted: May 9th, '08, 18:45 

Joined: Feb 28th, '07, 11:16
Posts: 40
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


Top
 Profile  
 
 Post subject: Re: Creating SEO Friendly URL redirector
PostPosted: May 12th, '08, 18:07 

Joined: May 12th, '08, 18:00
Posts: 4
I have a problem on a hosted server, I have a classifieds script with friendly URL, but doesn't work:

Code:
<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:
<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?


Top
 Profile  
 
 Post subject: Re: Creating SEO Friendly URL redirector
PostPosted: May 14th, '08, 23:15 

Joined: Feb 28th, '07, 11:16
Posts: 40
Place this in the file /home/www/pinrastro.es/www.pinrastro.es/.htaccess
Code:
<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>


Top
 Profile  
 
 Post subject: Re: Creating SEO Friendly URL redirector
PostPosted: May 15th, '08, 03:37 

Joined: May 12th, '08, 18:00
Posts: 4
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
###################################


Top
 Profile  
 
 Post subject: Re: Creating SEO Friendly URL redirector
PostPosted: May 15th, '08, 03:56 

Joined: May 12th, '08, 18:00
Posts: 4
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:

<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,
##################################################################


Top
 Profile  
 
 Post subject: Re: Creating SEO Friendly URL redirector
PostPosted: May 15th, '08, 11:23 

Joined: May 12th, '08, 18:00
Posts: 4
I forgot to say, Thank you for your help!!!

The problem whas on the server side.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 5 hours


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
Powered by phpBB