Welcome to WebmasterWorld Guest from

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Mod rewrite: Excluding directories when working with +two slashes

Excluding specific directories with Apache mod_rewrite

8:17 am on Jan 22, 2013 (gmt 0)

New User

5+ Year Member

joined:Jan 22, 2013
posts: 1
votes: 0

In short: I am working on a website where consumers can compare companies (service providers) in a several branches. Therefor we work with SEO friendly url's with max. 3 "directories":
. In these url's is 1 the branch within the user can search, 2 respresentates the city and 3 defines the company name.

I've done this with the following code in my .htaccess file.

<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On

RewriteRule ^(style|media)(/.*)?$ - [L]

RewriteRule index\.* index.php [L,QSA]
RewriteRule ^([^/]*)/([^/]*)/$ branch.php?branch=$1&city=$2 [L,QSA]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ company.php?branch=$1&city=$2&company=$3 [L,QSA]
RewriteRule ^([^/]*)/([^/]*)$ branch.php?branch=$1&city=$2 [L,QSA]
RewriteRule ^([^/][a-zA-Z0-9-]+)$ branch.php?branch=$1 [L,QSA]

My stylesheets are located in the directory style, and this works fine if you go to the webaddress
. The adresses
and further redirects to the right filename (branch.php or company.php), but keeps searching for stylesheet.css in
and so on). I know there are a lot of these questions on this site, and I tried many of their solutions, but none of them really does what I want.

I understand if this scenario is tough one, so I thought I would make it easier (but since this lang is confusing me, I don't really know what to do now lol). So here is what I need:
  • Every request has to redirect to index.php, with the given directories added as parameters. (so
    rewrites to index.php?1=1 and
    rewrites to index.php?1=1&2=a&3=b. In index.php I will lookup which page I have to include then).
  • Dirs style and media are excluded from this rule.
  • Not every excisting dir or file has to be excluded, because users may not reach the dir includes (for example)

    I hope you can help me, because I am making a huge mess of it ^^

  • 10:20 am on Jan 22, 2013 (gmt 0)

    Senior Member from US 

    WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time 5+ Year Member Top Contributors Of The Month

    joined:Apr 9, 2011
    votes: 598

    RewriteRule ^(style|media)(/.*)?$ - [L]

    What's the (/.*)?$ for?

    "If the request begins in 'style' or 'media', optionally followed by a directory slash, which in turn may be followed by more stuff before you get to the end, then capture the optional part and don't do anything with it."

    If "style" is a directory, as the later part of your question suggests, then the slash isn't optional at all. In fact you mean
    ^style/[^/.]+\.css - et cetera
    and the same presumably for /media/. It may or may not go in the same rule.

    RewriteRule ^([^/]*)/([^/]*)/$ branch.php?branch=$1&city=$2 [L,QSA]
    RewriteRule ^([^/]*)/([^/]*)$ branch.php?branch=$1&city=$2 [L,QSA]

    What's the difference between these two rules? The only thing I see is the final / which can be expressed as /? within a single rule. Except that you need to decide whether the slash is required or forbidden: one or the other, not both. In a redirect it wouldn't matter, but here you're rewriting.

    What's the QSA for? Is there any possibility that these incoming pretty URLs already have a query attached? Where would it have come from?

    When you say
    you mean
    because otherwise you could have null directories, or two consecutive slashes.

    In any case, you don't need to exclude stylesheets because you don't have extensions do you? The assorted rules are really based on one or more iterations of
    ... and, come to think of it, where do images get excluded? And all the other non-page material?

    RewriteRule index\.* index.php [L,QSA] 

    I don't understand this. QSA is the default so you don't need to say anything about it unless you have changed the query string by putting a ? in the target. As written, the rule says "if the request contains the word 'index' at any point, optionally followed by one or more literal periods, then serve content from index.php". Now, I really should not be answering posts at this hour of the night, but I am ### sure that is not what you meant the rule to say.

    Well. That's a start anyway.
    10:35 am on Jan 22, 2013 (gmt 0)

    Senior Member

    WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

    joined:July 3, 2002
    votes: 0

    RewriteRule ^(style|media)(/.*)?$ - [L]

    simplifies to
    RewriteRule ^(style|media) [L]

    as long as there are no other folder names beginning style... or media...

    matches a request for
    - change the * to + here and in the other rules.

    simplifies to
    unless you're saying that the first character of the request can be something other than a-zA-Z0-9-

    matches example.com/images/image.png. You probably want to add a "." in final grouping using
    instead (also * -> + as mentioned before). Make the same changes to the "3-deep" rule too.

    Rules 2 and 4 can be combined if the pattern ends with /?$ - just like you have already done for rule 3. Normally, you should pick one - "with slash" or "without slash" - as the canonical URL and redirect the other, but in this case as these are search URLs, maybe that doesn't matter so much.