Forum Moderators: phranque

Message Too Old, No Replies

Removing page name virtual directory via mod_rewrite

         

themoron1

10:03 pm on May 16, 2005 (gmt 0)

10+ Year Member



Sorry if this has been asked a million and one times, but I have written this code for my site

RewriteEngine On
RewriteRule ^index/(.*)/(.*)/ /index.php?cat=$1&item=$2
RewriteRule ^index/(.*)/(.*) /index.php?cat=$1&item=$2
RewriteRule ^index/(.*) /index.php?cat=$1
RewriteRule ^index/(.*)-(.*) /index.php?cat=$1&p=$2

which works fine. But I want to remove the /index so the address line looks like domain.tld/1/2 rather than domain.tld/index/1/2. I have tried simply removing the word index or index/ so the code starts ^/(.* or ^(.* but it doesn't work. Any ideas?

Cheers

themoron :@)

jdMorgan

11:18 pm on May 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's a speed-up, a fix for optional "index/" prefixes, and a fix for optional trailing slash, combined:

RewriteEngine On
RewriteRule ^(index/)?([^/]+)/([^/]*)/? /index.php?cat=21&item=$3 [L]
RewriteRule ^(index/)?([^/]*)/? /index.php?cat=$2 [L]
RewriteRule ^(index/)([^-]+)-([^/]*)/? /index.php?cat=$2&p=$3 [L]

  • Whenever possible avoid multiple (.*) patterns -- they are extremely inefficient.
  • Adding a "?" after a character or parenthesized expressions makes that character or parenthesized expressions optional.
  • Use the [L] flag unless you need to have the output of this rule processed by the following rules.

    I assumed that you want to be able to request URLs matching the patterns on the left without including the "index/" part when you type the URL into your browser. If that's not what you wanted, please explain in terms of requested URL versus content filepath.

    Jim

  • themoron1

    9:55 am on May 17, 2005 (gmt 0)

    10+ Year Member



    Thanks for the code but it doesn't work. :(

    500 Internal Server Error

    themoron :@)

    jdMorgan

    12:50 pm on May 17, 2005 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    > 500 Internal Server Error

    Please post the relevant lines from your server error log for this error.

    Jim

    themoron1

    3:06 pm on May 17, 2005 (gmt 0)

    10+ Year Member



    The complete error is:
    "Internal Server Error
    The server encountered an internal error or misconfiguration and was unable to complete your request.
    Please contact the server administrator, webmaster@########.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.

    More information about this error may be available in the server error log.

    Additionally, a 500 Internal Server Error error was encountered while trying to use an ErrorDocument to handle the request"

    And the error logs are

    [Tue May 17 15:26:06 2005] [error] [client ##.##.##.##] File does not exist: /home/username/public_html/404.shtml
    [Tue May 17 15:26:06 2005] [error] [client ##.##.##.##] File does not exist: /home/username/public_html/index

    Cheers

    themoron :@)