Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite question

want to make my url's look nicer

         

gilahacker

4:57 pm on Sep 9, 2005 (gmt 0)

10+ Year Member



ok, I'm using a CMS w/ a "friendly url" addon that makes urls like this:

h_ttp://www.mydomain.com/index.php?option=com_bookmarks&Itemid=0&mode=0&catid=6

look like this:

h_ttp://www.mydomain.com/bookmarks/list/onecat/Computer_and_Internet_Franchises/*/

which is better I guess...

I'm not entirely sure how it all works, but I want to know if it's possible to take that a step further to make the url look like this instead:

h_ttp://www.mydomain.com/Franchise_Directory/Computer_and_Internet_Franchises/*/

basically replacing the bookmarks/list/onecat with Franchise_Directory

I'd have to make changes to the directory component itself to make it spit out the new urls for the sub-categories... I don't think that's a real problem.

I don't want redirects that change the url after you hit enter (i.e. typing in h_ttp://mydomain.com and it changes to h_ttp://www.mydomain.com)

Is this possible? (hope I'm making sense here)

Thanks in advance!
-Jason

jd01

6:26 pm on Sep 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, if you can change the way the links are created to the new URL you would like, it is possible, but you will have to post your current rule(s) for the location you would like to change, or I would just be guessing to show you how.

Justin

gilahacker

8:28 pm on Sep 9, 2005 (gmt 0)

10+ Year Member



ok, the module that does the rewriting basically acts as a php based translator to turn unfriendly urls into friendly ones, and friendly ones back into ones the CMS can understand.

I know it does use .htaccess somehow...

here is my .htaccess file, without comments or my personal "redirect 301" rules that do not apply to this:

Options +FollowSymLinks
RewriteEngine On

<IfModule mod_php4.c>
php_value include_path ".:/usr/local/lib/php:/home/public_html/"
php_value session.save_path "/home/public_html/tmp"
php_value session.auto_start "On"
</IfModule>

RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^(.*) index.php

I don't know if the mod_php4.c section affects it or not... I don't honestly know what that's for... it looks like something to do with php sessions, probably for the statistics module

-Jason

jd01

8:40 pm on Sep 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, I am guessing you have not installed the friendly URL add-on yet, because the current file basically does nothing, except make it so you will never serve a 404 (bad) and increase server load by checking every single request to see if it is actually a file or a driectory (bad again).

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php

This says for every request, check the entire file tree to see if the request is an actual file, and if it is not, check the entire file tree to see if the request is an actual directory, if it is neither, serve them the information from the index.php page...

You would be much better off with a custom error page:
ErrorDocument 404 /yourfile.php

IOW

ErrorDocument 404 /thepath/toyour/file.yourextention

You will need to find the .htaccess information that comes with the add-on, before we can help you there.

The <IfModule mod_php4.c> module is only for php / sessions, has nothing to do with URLs and can actually go above the Options and RewriteEngine lines to keep them separate -- does not matter too much, but is better practice to keep things separate.

Justin

gilahacker

9:05 pm on Sep 9, 2005 (gmt 0)

10+ Year Member



I don't know why it's using

RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^(.*) index.php

instead of a 404 redirect... maybe the intent was to not return 404 errors to the search engines? I've got a pretty big directory tree, so yeah... this could definitely be slowing things down.

I stickymailed you my whole .htaccess file... there doesn't appear to be anything in it other than what I posted, a whole bunch of manual redirects I've entered, and a line to let it parse php in html files.

The friendly url thingy is turned on and working, but it appears to be doing everything via php. I'm not even sure WHY it requires mod_rewrite...

I'm going to piss into the wind a bit here, but my thinking is this:

that part in the .htaccess file about it checking to see if the request is a file or directory is the friendly url thing...

it's saying that if the request is not an existing file or directory, then it directs the request to index.php

index.php calls the friendly url thingy and passes it the requested url

the friendly url thing translates (using php) the "friendly" url back into something the CMS can understand and passes it back to the CMS.

Am I totally off base here?

Thanks again,
-Jason

[someone please tell me I can take a class on this crap at the community college down the road?]

jd01

9:20 pm on Sep 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Just replied to your mail...

The php summary appear to be correct.

If your are using this CMS for most of your files, you would be much more efficient to define your specific directories:

So, if you are using the CMS for most of the site, but have static files in yourdir/ somedir/ and anotherdir/ you could use this:

RewriteCond %{REQUEST_URI}!/(yourdir¦somedir¦anotherdir)/
RewriteRule ^(.*) index.php [L]

Added: (Hit the wrong button)

OR if the CMS is all in one dir like /blog/ you could use this:

RewriteCond %{REQUEST_URI} /blog/
RewriteCond %{REQUEST_URI}!index\.php
RewriteRule ^(.*) index.php [L]

Condition 1: If the request is for blog
Condition 2: If the request is not for index.php

Result: Rewrite to index.php

Justin

BTW This is the community college down the road =) Check out the library -- links at the top left of the pages.

Done Editing Now -- Can't type what in my head sometimes.

jdMorgan

9:40 pm on Sep 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I recommend that you don't use the ErrorDocument approach, since that does indeed return a 404 for each script-processed request, and can cause problems with search listings.

But I would recommend that you look into restricting the number of files that must be processed by index.php.

There are a couple of ways to do this; Use one or both.

1) Restrict calls to index.php by URL. This can be as simple as making your 'friendly' URLs include an extra subdirectory path-part for URLs that need to be processed by the script, such as example.com/items/<friendly_stuff_here>. index.php is then called only for URL-paths starting with "items".

2) Restrict calls to index.php by filetype. You may not need to process every request for images, stylesheets, and external JavaScripts through index.php. Some of these may not even need the friendly-URL treatment. Here, you could exclude all image, stylesheet, and JS script requests from being rewritten to index.php.

In other words, product and category page URLs need to be 'friendly' but maybe these others don't. I certainly recommend that you do not pass requests for robots.txt and ErrorDcouments to index.php -- Doing so can cause catastrophic problems because of tiny errors in index.php or even temporary databse connectivity problems. The more you restrict calls to index.php, the less often you have to do all the file- and directory-exists checking.

What jd01 says is correct; running file-exists and directory-exists checks on each and every request negatively impacts server performance. But if you're making money off the site, then maybe a dedicated or higher-performance server would be preferable to modifying your approach -- Just trying to see both sides, here.

Another thing to check is to be sure that if you make a nonsense call to index.php, that it will return a 404-Not Found. Several major search engine spiders don't like infinite URL-spaces; They test for this and will reduce crawling depth on sites that never return a 404.

Jim

jd01

9:47 pm on Sep 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I recommend that you don't use the ErrorDocument approach

I agree - did not realize at the time they were using index.php to serve all requests that were not specifically uploaded by a user.

Better to find a way to limit, or like jdMorgan said -- unexpected or undesireable results may ensue.

Justin