Forum Moderators: phranque
My site is informational as well as a store. I'm using CuteNews as a news script for the main site, and a modification that that uses ModRewrite to change news URLs to the title of the article. I'm also using a shopping cart package that uses ModRewrite to rewrite store pages to search engine friendly URLs. I'm not really that knowledgeable with .htaccess files, and I'm having a problem with mine.
Basically, whenever someone visits a link like "domain.com/catalog/" or, in other words, linking directly to a category folder, they are just taken to (broken) version of my home page (home.php). I have index.php files set up in those directories, but something about my .htaccess is causing a conflict and redirecting all category folders. I hope this makes sense.
Anyway, here is my .htaccess code. I don't really understand it all and I know some might not be necessary, so if you could help me, I'd really appreciate it. Thanks a lot in advance!
DirectoryIndex /home.php
ErrorDocument 404 /404.php# NEWS SCRIPT RELATED
RewriteEngine On
RewriteBase /
RewriteRule ^post/(.*).html(.*)$ home.php?subaction=showfull&url=$1$2 [L,NC]
RewriteRule ^postpage/(.*).html(.*)$ home.php?start_from=$1$2 [L,NC]
RewriteRule ^comments/(.*).html(.*)$home.php?subaction=showcomments&url=$1$2 [L,NC]
RewriteRule ^commentspage/(.*)/(.*).html(.*)$index.php?comm_start_from=$1&subaction=showcomments&url=$2$3 [L,NC]
RewriteRule ^archiveindex/(.*).html(.*)$home.php?archive=$1&subaction=list-archive$2 [L,NC]
RewriteRule ^archive/(.*).html(.*)$home.php?subaction=showfull&url=$1$2 [L,NC]
RewriteRule ^archivepage/(.*)/(.*).html(.*)$home.php?start_from=$2&archive=$1&subaction=list-archive$3 [L,NC]
# REWRITE RULES
Options +SymlinksIfOwnerMatch -Indexes
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST}!^www\.domain\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
rewriteRule (.*) http://www.domain.com/$1 [R=301,L]
# STORE RELATED
RewriteBase /
RewriteRule ^([^/]*/)?[^/]+p[-_]?([0-9]+)\.html$$1index.php?target=products&product_id=$2[L]
RewriteRule ^([^/]*/)?[^/]+c[-_]?([0-9]+)([-_]sort[-_]([a-z0-9_]+)[-_]order[-_]([a-z0-9_]+))?([-_]page-([a-z0-9_]+))?\.html$
$1index.php?target=categories&category_id=$2&sort_by=$4&sort_order=$5&page=$7[L]
RewriteRule ^([^/]*/)?[^/]+a[-_]?([0-9]+)\.html$ $1index.php?target=pages&page_inner_id=$2[L]
RewriteRule ^([^/]*/)?[^/]+t[-_]?([0-9]+)\.html$ $1index.php?target=topics&topic_id=$2[L]
RewriteRule ^([^/]*/)?[^/]+m[-_]?([0-9]+)([-_]sort[-_]([a-z0-9_]+)[-_]order[-_]([a-z0-9_]+))?([-_]page-([a-z0-9_]+))?\.html$
$1index.php?target=manufacturers&manufacturer_id=$2&sort_by=$4&sort_order=$5&page=$7[L]
RewriteRule ^([^/]*/)?form[-_]?([a-z0-9_]+)[-_]?(sent)?\.html$ $1index.php?target=forms&name=$2&mode=$3[L]
RewriteRule ^([^/]*/)?manufacturers\.html$ $1index.php?target=manufacturers[L]
RewriteRule ^([^/]*/)?catalog\.html$ $1index.php?mode=catalog[L]
RewriteRule ^([^/]*/)?index\.html$ $1index.php[L]
# GENERAL
RewriteCond %{HTTP_REFERER}!^http://(.+\.)?domain\.com/ [NC]
RewriteCond %{HTTP_REFERER}!^https://(.+\.)?domain\.com/ [NC]
RewriteCond %{HTTP_REFERER}!^$
RewriteRule .*\.(jpe?g¦gif¦bmp¦png¦mid¦mp3)$ - [F]
</IfModule>
Most dangerous is the repeated, and therefore unconditional, second instance of
rewriteRule (.*) http://www.example.com/$1 [R=301,L]
I would expect this to put your server in a loop and cause the client to give up for any non-news URL whatsoever.
However, I see nothing in the rules you've got that would rewrite or redirect the requested URL "domain.com/catalog/" itself.
So, that would imply that such a "/catalog/" URL would fail, produce a 404, and --no matter what directory (e.g. "/catalog/") that URL specified-- the server would therefore serve domain.com/404.php *unless* you have ErrorDocument 404 directives in .htaccess files in the requested subdirectory that override the ErrorDocument 404 directive in your main .htaccess file.
Jim
[edited by: jdMorgan at 5:36 pm (utc) on Aug. 20, 2007]
Thanks for your reply. When refer to the "dangerous" code, are you referring to these two lines?
RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301,L]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
I notice they aren't quite the same, but should one or both be removed?
I'm sorry to hear the code is so bad. Most of it was written automatically by my news client and shopping cart software. The rest was taken from other places on the net to do things like redirect all traffic from "http://domain.com" to "http://www.domain.com."
As for my problem, no matter what directory I link to on my server (i.e. "domain.com/folder/" or "domain.com/folder2/" etc., I'm taken back to my homepage, not a 404 page. Can you think of any reason why that might happen? If not, I appreciate your time anyway.
Unless that error is also causing the other problem, I don't know why your directories redirect to the home page -- None of the other rules in this file will match a "directory-only" URL, so none of them can be doing that redirect. You might want to look at the code in your configuration files, in your subdirectory .htaccess files,
and even examine your scripts themselves to see if they might be doing the redirect internally.
Jim
Thanks again for the advice. Based on your recommendation of checking the .htaccess files in the subdirectories, I simply added the line "DirectoryIndex index.php" to the htaccess file in folders that already had one. For those directories that didn't have one, I added one using just that line. This seems to have corrected the problem. Is there anything wrong with using this solution?