Rewrite Paths AND File Paths Working Together

Hi all. I’ve run into a problem that despite hours of googling, I can seem to resolve.

I’ve got the following file structure:

/ index.php
/properties/

Now I’m using the following rewrite rules to make “properties/123456” rewrite to "properties/showprop.php?id=123456. All works just fine for that. The problem lies when I’m trying to open a physical file actually IN the properties folder, ala “/properties/res.php”. The rewrite rule is trying to interpret “res” as a variable. This below did NOT work.

RewriteCond %{REQUEST_URI} !^/properties/res\.php$
RewriteRule ^properties/([0-9a-zA-Z-]+) properties/showprop.php?id=$1 [QSA]

So, how can I exclude that one particular file?

Try this:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^properties/([0-9a-zA-Z-]+) properties/showprop.php?id=$1 [QSA]

This will prevent rewriting for files that actually exists

Still not working. Here’s a look at all rewrite directives. Perhaps something is interfering:

RewriteEngine On
Options -MultiViews

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^properties/([0-9a-zA-Z-]+) properties/showprop.php?id=$1 [QSA]
    RewriteRule ^news/([0-9a-z_-]+) news/shownews.php?id=$1 [QSA]
    RewriteRule ^video/([0-9a-zA-Z_-]+) video/showvid.php?id=$1 [QSA]
    RewriteRule ^newsletters/([0-9a-z_-]+) newsletters/showletters.php?id=$1 [QSA]
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^(.*)$ $1.php [NC,L]

To make matters worse, as you can see - i’m removing the .php extensions and I think that is factoring into it as well.

Yes, it does.
Try to replace all [QSA] with [QSA,L]

bv,

It looks like you’re using DIGITS when redirecting to showprop.php and lowercase LETTERS when redirecting extensionless filenames to the actual PHP files. IF that’s the case, make your character range definitions match ONLY the correct set of characters, i.e., [0-9]+ for digits and [a-z]+ for lowercase letters. That’s the simple version of an answer that would solve all the problems presented.

Also, I like MZ’s comment about using the Last flag.

Once again, though, if you would like to learn more about mod_rewrite, I created a rather lengthy tutorial years ago (as a Mentor … before my Team Leader days) and, although it was abbreviated by SitePoint, I still have the long version (with examples) online at http://datakoncepts.com/seo.

Regards,

DK

DK -

showprop.php could be fed either letters OR digits depending on the property i want it to show, so limiting the set of characters is not an option for me. However, I did find THIS snippet that seemed to work:

RewriteCond %{REQUEST_URI} !^/properties/res

I’ve now moved onto another curious problem thats got me baffled. I’m using the exact same rewrite rule, save for the directory, and for some reason it’s appending the search query onto the rewritten url, ala:

RewriteCond %{REQUEST_URI} !^/neighborhoods/index
RewriteRule ^neighborhoods/([a-z-]+) neighborhoods/shownei.php?id=$1 [QSA,L]

So then what should look like this:

www.website.com/neighborhoods/cinco

is being rewritten like so:

www.website.com/neighborhoods/cinco/?id=cinco

And that confuses the hell outta me. Any ideas?

bv,

mod_rewrite will make multiple passes until it finally gets resolved. The first pass would change neighborhoods/cinco to neighborhoods/shownei.php (and a query string which included id=cinco). The next pass would have matched neighborhoods/shownei (without the .php - no end anchor) and redirect to neighborhoods/shownei (adding id=shownei to the query string). The third pass would do the same thing as well as the fourh, etc, until Apache determined that it would never stop. I don’t know how it would add the / because you don’t show any competing mod_rewrite.

Try adding a $ to the atom in the RewriteRule (to prevent a match before the file extension).

Regards,

DK

DK -

Thanks. The /$ killed off the appended search query, but it’s still appending the trailing slash, which of course is through off all the linked images, etc. If I add JUST $ (no /) I’m still left with the appended query. Here’s the entire htaccess file save for gzip / expires headers / etags. See anything that might be forcing that trailing slash? Cause I don’t.

Options -Indexes +FollowSymLinks
DirectoryIndex  index.php default.php default.htm default.html index.htm index.html
ErrorDocument 404 /amberleafrealty.com/404.php
FileETag none

RewriteEngine On
Options -MultiViews

RewriteCond %{REQUEST_URI} !^/properties/index
RewriteCond %{REQUEST_URI} !^/properties/res
RewriteCond %{REQUEST_URI} !^/properties/com
RewriteCond %{REQUEST_URI} !^/properties/search
RewriteCond %{REQUEST_URI} !^/properties/app
RewriteRule ^properties/([0-9a-zA-Z-]+) properties/showprop.php?id=$1 [QSA,L]
RewriteCond %{REQUEST_URI} !^/neighborhoods/index
RewriteRule ^neighborhoods/([a-z-]+)/$ neighborhoods/shownei.php?id=$1 [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

RewriteCond %{HTTP_HOST} ^amberleafrealty.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.amberleafrealty.com$

bv,

That should never have been /$ as only $ was intended. With your code, that should happen twice.

Try:

Options -Indexes +FollowSymLinks
DirectoryIndex  index.php default.php default.htm default.html index.htm index.html
ErrorDocument 404 /amberleafrealty.com/404.php
FileETag none

RewriteEngine On
Options -MultiViews

RewriteCond %{REQUEST_URI} !^properties/(index|res|com|search|app)
RewriteRule ^properties/([0-9a-zA-Z-]+)$ properties/showprop.php?id=$1 [QSA,L]

RewriteCond %{REQUEST_URI} !^/neighborhoods/index
RewriteRule ^neighborhoods/([a-z-]+)$ neighborhoods/shownei.php?id=$1 [QSA,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule .? %{REQUEST_URI}.php [L]

# Not associated with any RewriteRule!?!
# RewriteCond %{HTTP_HOST} ^amberleafrealty.com$ [OR]
# RewriteCond %{HTTP_HOST} ^www.amberleafrealty.com$

Regards,

DK

DK -

Still appending the query on the end and thinking it’s a folder for some reason. Check it out at:

http://www.amberleafrealty.com/neighborhoods/

And click on any one of the neighborhoods.

Thanks for all your help!
Brian.

bv,

Oh, my! I believe that you are correct about Apache believing the URI is for a directory but I’ve never seen a case like this.

  1. Move -MultiViews up with the other Options at the top (and I believe that +FollowSymLinks should already be set in the server’s config file so I’d try eliminating that, too).

  2. I’m not sure what FileETag does (I’ll look it up later) but try commenting that line out.

  3. To combat the server’s adding the trailing / to your links, add as the FIRST RewriteRule set after RewriteEngine on:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(properties|neighborhoods)/([a-z-]+)/$ $1/$2 [L]
# OR flag of [R=301,L] for testing vitibility

For visual clarity, I separate my rule sets (RewriteCond and associated RewriteRule) from each other with an additional line) that would have shown you that the two RewriteCond statements at the end were without an associated RewriteRule.

Regards,

DK

1 Like

DK -

I think at long last that has fixed it. Thank you SO much. I can now proceed to glue all my hair back on top of my head. I appreciate all your help!

I wish it were only that easy! :smiley:

You’re very welcome. I’ve been here for too many years to admit to in the belief that one must “share the knowledge.” I can only hope that I have passed that mantra along to others.

Regards,

DK

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.