Forum Moderators: phranque

Message Too Old, No Replies

Trailing Slash Problem (mod_rewrite)

Does not work under one specific directory

         

ebizcamp

10:35 pm on May 18, 2004 (gmt 0)

10+ Year Member



Hi,

I have a website, there are two directories under the root directory, one /abc and the other /xyz

I have a script which sydicates with allposters to generate an allposters affiliate website, it is written in php and there are several variables, like php?cat=****&sub=yyy ...

I have modified the script and use mod_rewrite to generate static pages. So my URL now is in this format /abc/123/456/ instead of /abc/php?cat=123&sub=456

The problem is that, whenever I visit mysite/abc I get this error:

Error: Please use the domain-name instead of the IP-address!

But it's okay if browsing mysite/abc/

If I visit mysite/abc/123, I get 404: File not found error.But it is okay if I browse mysite/abc/123/

It is weird. Because Trailing Slash is supposed not to be a problem. Say, when browsing mysite/xyz, it is okay.

I add lines below to my htaccess:

RewriteEngine on
RewriteBase /abc/
RewriteRule ^(.*)$ $1/
RewriteRule ^(.+)$ $1/ [L]

But the problem still exists. Any idea? Thanks

gergoe

11:08 pm on May 18, 2004 (gmt 0)

10+ Year Member



This depends on numerous things;

Trailing slashes are not problem as long as you don't use mod_rewrite, or if you use it, you use it only for (fully qualified or absolute) external redirection and for proxy requests, but if you start using mod_rewrite to rewrite the urls, then you have to be carefull with them. I sugegst you to post all of your rewriting settings, this sounds as if it were something wrong in the one which translates the /abc/123/456/ url into /abc/php?cat=123&sub=456. by the way, what's the /abc/php? It is a typo, or an another rewriting?

ebizcamp

3:13 am on May 19, 2004 (gmt 0)

10+ Year Member



Gergoe,

Thanks for reply. php? should be script.php?

My htaccess file is:

Options +FollowSymlinks
RewriteEngine on
RewriteBase /abc/
RewriteRule (.+)/ script.php?var1=$1
RewriteRule (.+)/(.+)/ script.php?var=$1&var2=$2
RewriteRule id-(.+)\.html another_script.php?id=$1 [L]

ebizcamp

3:30 am on May 19, 2004 (gmt 0)

10+ Year Member



It is from my log:

11.22.11.22 - - [18/May/2004:23:19:39 -0400] "GET /abc HTTP/1.1" 400 2166 www.mysite.com "-" "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0; Alexa Toolbar)" "-"

Return code is 400

If try this URL: www.mysite.com/abc/123

Then the return code is 404

jdMorgan

3:55 am on May 19, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A few points to consider:

  • To prevent your first rule from being applied to *all* URLs, you need to re-arrange the order of your rules.
  • Unless you want the output of the first rule to be tested and processed by subsequent rules, use [L] on all of them -- [L] only stops mod_rewrite processing if the rule it is associated with matches and that rewrite is invoked. If your rules are mutually-exclusive, use [L] to avoid wasting CPU time.
  • Anchoring your patterns using "^" and "$" speeds up processing. However, you may have to leave the start anchor off if your URLs point to various directory levels, i.e. if not all URLs refer to /abc/id- or /abc/var1/ or /abc/var1/var2/, but maybe /abc/def/var1/var2/ as well.
  • Using a forward-looking match pattern like [^/] speeds up processing. (Note that all characters x in group patterns like [^x] are treated as literal characters: "[^.]+" means "one or more characters *not* equal to a period." Inside [], there are no "special" characters except "]", and in some cases, "-".)
  • A "?" following "/" will make "/" optional.

    This might work better - or maybe not:


    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /abc/
    RewriteRule ^id-([^.]+)\.html$ another_script.php?id=$1 [L]
    RewriteRule ^([^/]+)/(.+)/?$ script.php?var=$1&var2=$2 [L]
    RewriteRule ^(.+)/?$ script.php?var1=$1 [L]

    Jim
  • ebizcamp

    5:16 am on May 19, 2004 (gmt 0)

    10+ Year Member



    Thanks, Jim.