Forum Moderators: phranque

Message Too Old, No Replies

.htaccess beginner's problem

/ doesn't show contents of index.php

         

adrianTNT

5:34 pm on Aug 6, 2007 (gmt 0)

10+ Year Member Top Contributors Of The Month



Hello, I have these rules to rewrite browsing some listing categories (/category_name/) and then listings details pages (/category/listing_title/).

================================================
Options +FollowSymLinks
RewriteEngine On

RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteCond %{REQUEST_FILENAME}!-l

# this will show listings by /category/
RewriteRule ^([A-Za-z0-9-]+)/?$ browse.php?cat_title=$1 [L]
RewriteRule ^([A-Za-z0-9-]+)?$ browse.php?cat_title=$1 [L]

# this will show listings with pagination: /category_2/
RewriteRule ^([A-Za-z0-9-]+)_([0-9]+)/?$ browse.php?cat_title=$1&pageNum_Recordset_listings=$2 [L]
RewriteRule ^([A-Za-z0-9-]+)_([0-9]+)?$ browse.php?cat_title=$1&pageNum_Recordset_listings=$2 [L]

# this will rewrite the details for the listings /category/listing_title_10.html
RewriteRule ^(.*)/(.*)_(.*).html$ browse_details.php?listing_id=$3
================================================

My only problem is that when I access site root / I do not see the contents of index.php but the contents of browse.php or browse_details.php, this is because it probably thinks that "/" is the beginning of a category like "/category_name/".
How to make / show contents of index.php?

Any help is appreciated.
Thank you.
- Adrian.

[edited by: adrianTNT at 5:41 pm (utc) on Aug. 6, 2007]

jdMorgan

7:04 pm on Aug 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



"/" won't match any of your patterns, so it's more likely that your DirectoryIndex directive is missing "index.php" as a candidate index page, or that the entire directive is missing. See Apache mod_dir.

Jim

adrianTNT

8:38 pm on Aug 6, 2007 (gmt 0)

10+ Year Member Top Contributors Of The Month



You mean that index.php is not shown by default? It is. If I remove the url rewrite rules than / will show index.php.

So I think there is something I need to change in the rules, or something to add so that index.php is shown at /

jdMorgan

9:38 pm on Aug 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, I see it now. It's the second rule, which has the pattern ^([A-Za-z0-9-]+)?$
The question mark near the end makes the entire preceding parenthesized sub-pattern optional, so this will match a blank URL-path, which is what you'll get with a request for "/".

I'd suggest removing that question mark (but read on). If you eliminate that question mark, then the first and second rule become equivalent, and the second rule can be deleted. Same thing for the third and fourth rule.

But, also be aware that your initial three RewriteConds will only apply to the first rule that follows them. Matching rules after that first rule will be applied whether the requested URL-path resolves to an existing file, directory, or symbolic link. That's probably not what you want, and that is a contributing cause of this problem.

I'd suggest re-structuring those RewriteConds like this:


RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule .* - [S=3]

This will leave the URL-path unchanged, but Skip the following three rules if the requested URL-path exists as a file, directory, or symbolic link. You have a choice between doing it this way, which means you must update the skip-count if you add or remove rules, or of replicating the "exists check" RewriteConds for each rule.

"Exists checks" are horribly inefficient, and I suggest that you make the patterns in the skip rule as specific as possible, so that the checks are not done unless absolutely necessary. However, the pattern must take into account all of the patterns of the following rules, and since the last rule is very ambiguous, you may be running a lot of checks that you don't need to.

Fixing various other ambiguities and inefficiencies, I'd suggest:


Options +FollowSymLinks
RewriteEngine On
#
RewriteCond %{REQUEST_URI} !^([a-z0-9-]+/?¦.+/[^_]+_[^.]+\.html)$ [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule .* - [S=3]
#
# this will show listings by /category/
RewriteRule ^([a-z0-9-]+)/?$ browse.php?cat_title=$1 [NC,L]
#
# this will show listings with pagination: /category_2/
RewriteRule ^([a-z0-9-]+)_([0-9]+)/?$ browse.php?cat_title=$1&pageNum_Recordset_listings=$2 [NC,L]
#
# this will rewrite the details for the listings /category/listing_title_10.html
RewriteRule ^(.+)/([^_]+)_([^.]+)\.html$ browse_details.php?listing_id=$3

Note that using the [NC] flag makes pattern compares case-insensitive, eliminating the need to use [A-Za-z] and allowing the use of [a-z] only. It's a full one-third faster. Using forward-looking negative matches such as the "[^_]+" and "[^.]+" (meaning "match up to the next underscore or match up to the next literal period") is also more efficient and faster.

Replace all broken pipe "¦" characters above with solid pipes before use; Posting on this forum modifies the pipe characters.

Jim
[edit] Correction as noted in following post [/edit]

[edited by: jdMorgan at 8:12 pm (utc) on Aug. 7, 2007]

adrianTNT

3:33 pm on Aug 7, 2007 (gmt 0)

10+ Year Member Top Contributors Of The Month



Thanks Jim for the detailed instructions, I first tried the exact lines that you posted (replaced pipes too) but that didn't work, the first line you mentioned was a "Condition" not a "Rule"? It said "Server Error" before I replaced it with a "Condition" but some other things didn't work. Then I just removed the "?" in my rules and it now seems to work fine, I thought that was needed there so that I can access the directories with or without the end slash / but it works OK without the "?".
Im very new to this url rewrite so most lines ware taken from existent examples online.
It all seems to work now.

Thanks again.
- Adrian.

[edited by: adrianTNT at 3:34 pm (utc) on Aug. 7, 2007]

jdMorgan

8:14 pm on Aug 7, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



the first line you mentioned was a "Condition" not a "Rule"? It said "Server Error" before I replaced it with a "Condition"

Yes, well-spotted! I must have been having a "typo day." I corrected this in the post above, so others who don't read this far won't have the same problem.

Jim