Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

mod_rewrite problem, how to exclude text from the rule

how to exclude some text from the mod rewrite rule



11:33 am on Jul 20, 2005 (gmt 0)

10+ Year Member


i m using the following rule and i m sure it can be much better, i ve very lil experience with rewriting. so help.

i want my index.php file to open when typed www.domain.com/some-directory/
but i ve defined some rule to rewrite any page like this some-directory/some-page.php to this some-directory/services.php?name=(some-page)

RewriteRule some-directory/(.*)$ /services.php?name=$1

problem is above defined rule takes every request to the services.php page, i ve tried the following rule too
RewriteRule some-directory/[^(index)](.*)$ /services.php?name=$1
but i ve not been able exclude the index.php from being processed. can someone plz tell how to group the text correctly and exclude it.


1:54 pm on Jul 20, 2005 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member


Welcome to WebmasterWorld!

Just add a Rewrite Condition ahead of the rule to exclude /some-directory/index.php from being rewritten:

RewriteCond %{REQUEST_URI} !^/some-directory/index\.php$
RewriteRule ^some-directory/(.*)$ /services.php?name=$1 [L]

I assume that your code is in .htaccess. For use in httpd.conf, the above will need to be modified.



5:55 am on Jul 21, 2005 (gmt 0)

10+ Year Member

thank u so much for the quick reply, code worked fine and yes i 'm using it in .htaccess file.

there was a lil problem though, code worked fine for index.php at the end of path but it did not work when there was only domain/some-dir/
i had to add some to check the '/', modified code was

RewriteCond %{REQUEST_URI}!^/some-dir/index\.php$
RewriteRule ^some-dir/[^/](.*)$ /services.php?name=$1 [L]

even though its working fien now but is there a better way to do it?

btw, thanks for welcoming. :)


2:52 pm on Jul 21, 2005 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member

Modify the RewriteCond to make "index.php" optional:

RewriteCond %{REQUEST_URI} !^/some-directory/(index\.php)?$
RewriteRule ^some-directory/(.+)$ /services.php?name=$1 [L]

The RewriteCond will now match either "/index.php" or "/".



4:12 am on Jul 22, 2005 (gmt 0)

10+ Year Member

thanks, worked like charm.
another question arises here, what if i 've to make another file in the directory to exempt the rule, for instance, contact.php and/or about.php other than index.php

Featured Threads

Hot Threads This Week

Hot Threads This Month