Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite scenario feedback

         

krt1

12:35 pm on May 24, 2005 (gmt 0)

10+ Year Member



Is this is good idea?
To have:
RewriteRule ^(.*)$ index.php?p=$1

Then on index.php, have a file_exists check on $_GET['p'] and if true, use a header redirect to the page
Then if not, process the query string to redirect the user to an article or so for the general benefits of mod_rewrite - cleaner URL, SE friendly...

Other Benefits:
- Site easier to use
- No 404 errors

The only disadvantage I can think off is a small delay (because it calls index.php (which will be tiny (1kb or less)) and then the page that the redirect will go to.

Any feedback?

dcrombie

1:03 pm on May 24, 2005 (gmt 0)



infinite loop [google.com]

;)

jdMorgan

2:30 pm on May 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To expand on that just a bit, you need to prevent requests for index.php from being rewritten:

RewriteCond $1 !^/index\.php$
RewriteRule ^(.*)$ index.php?p=$1 [L]

Also, I suggest that you do not "redirect to", but rather "include" the desired page output. A redirect involves sending a message back to the browser (or client) that says, "The resource you asked for is at a different URL. Ask me for it again using this new URL." So every redirected transaction requires *two* requests from the browser. If you redirect every page request then your site will be extremely inefficient and slow, and your search engine rankings will likely suffer, because the requested resource will never be at the directly-linked URL.

Jim

krt1

5:49 am on May 25, 2005 (gmt 0)

10+ Year Member



dcrombie, I am aware of that ;)

Thanks for your suggestion jdMorgan, I will go with the includes.

dcrombie

9:26 am on May 25, 2005 (gmt 0)



I think you'd be better off using a 'custom 404 page' rather than handling every request with a PHP call. It's a big temptation to do everything in PHP, but there's a reason that Apache is the most popular server, and always more features to uncover and exploit.

krt1

9:53 am on May 25, 2005 (gmt 0)

10+ Year Member



Even better! Never thought of that
Thanks a lot :)

krt1

11:08 am on May 25, 2005 (gmt 0)

10+ Year Member



How can you get the data off the URL that caused the 404 error?

Eg. If I type in [example.com...] how can I get nothing.php in the query string

ErrorDocument 404 /index.php?q=%{PATH_INFO}

I want something like that with nothing.php being the PATH_INFO

So it should generate
ErrorDocument 404 /index.php?q=nothing.php

Thanks,
Taner

jdMorgan

12:49 pm on May 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Not to throw a big wet towel on this, but if using a Custom 404 Error page for content handling results in the client seeing a 404 when content actually exists, then I think that's search engine ranking suicide. Don't return a 404 response code to the client unless you mean it. It's a fundamental HTTP protocol violation, and can cause major problems.

This '404 content handling' method was one of those things that was invented by users of restrictively-configured shared hosting, as a work-around for the lack of mod_rewrite, cgi-scripting, etc. If you have any alternatives, I strongly suggest you use them.

IMHO, of course. ;)
Jim

krt1

10:11 pm on May 25, 2005 (gmt 0)

10+ Year Member



grr.. good point though :)

I will explain my problem in detail later, I have to go now...

- LIVERPOOL! '05 European Champions

krt1

6:12 am on May 26, 2005 (gmt 0)

10+ Year Member



Here is my current situations:

I have a site where I have some folders and files
I also want users to be able to use the URL for easier site access, bookmarking and for SEO

So I would have two types of URLs

Some where the page actually exists eg.
http://example.net/forums
http://example.net/img/header.png

And some that will be processed
eg. http://example.net/php/security/sql
-> /index.php?p=/php/security/sql
which will then incldue the article for the sql page in the category PHP and sub-category security

I have thought of these methods:

1. Requiring a /q/ or similar before the URL of pages that will be rewrited.
eg. http://example.net/q/php/security/sql
-> /index.php?p=/php/security/sql

2. Define exceptions
eg.
RewriteCond %{PATH_INFO} ^!(forums¦img)
RewriteCond %{PATH_INFO}!\.[[:alpha:]]{2,5}$

3. Custom 404 errors but these won't work as these are not SE friendly as the above member wrote

Any other methods or suggestions to the above?

Thanks for any help in advance
Taner

[edited by: jdMorgan at 6:25 am (utc) on May 26, 2005]
[edit reason] No URLs, Please see TOS. [/edit]

jdMorgan

8:50 pm on May 26, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Using a key, such as the "q" you mentioned above will be fast.
Putting the files in different directories would also be fast -- You can put the rules in the.htaccess in those directories; then they won't need to be processed for all requests.
Making an exclusion list is OK, but then you have to maintain it.
Checking for "file exists" is very slow and inefficient, and should only be used if combined with one of the methods above. Otherwise, you're asking your server to search the filesystem for each HTTP request.

It all comes down to which method you feel works best for your site.

Jim

krt1

11:15 pm on May 26, 2005 (gmt 0)

10+ Year Member



I really want this stated so I'll go with the key eg. having q/...