homepage Welcome to WebmasterWorld Guest from 54.197.111.87
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Mod rewrite: Excluding directories when working with +two slashes
Excluding specific directories with Apache mod_rewrite
Ronald01990




msg:4538259
 8:17 am on Jan 22, 2013 (gmt 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":
mydomain.com/1/, mydomain.com/1/2/ and mydomain.com/1/2/3. 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]
</IfModule>


My stylesheets are located in the directory style, and this works fine if you go to the webaddress
mydomain.com or mydomain.com/1. The adresses mydomain.com/1/ and further redirects to the right filename (branch.php or company.php), but keeps searching for stylesheet.css in mydomain.com/1/style/ (or mydomain.com/1/2/style 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 domain.com/1/ rewrites to index.php?1=1 and domain.com/1/a/b 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 ^^

    Thanks!

  •  

    lucy24




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

    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.

    g1smd




    msg:4538292
     10:35 am on Jan 22, 2013 (gmt 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 example.com/// - change the * to + here and in the other rules.

    ^([^/][a-zA-Z0-9-]+)$ simplifies to ^([a-zA-Z0-9-]+)$ 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.

    Global Options:
     top home search open messages active posts  
     

    Home / Forums Index / Code, Content, and Presentation / Apache Web Server
    rss feed

    All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
    Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
    WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
    © Webmaster World 1996-2014 all rights reserved