Link to home
Start Free TrialLog in
Avatar of bennybutler
bennybutler

asked on

Need mod_rewrite settings

Ok, I don't know regex, not mod rewrite... I'm doing something I think mod_rewrite can handle, but I don't have alot of time and the docs aren't sinking in.

I want this in an htaccess file in the root of the website.

When a user visits
http://www.mysite.com/bob/  (which bob doesn't exist)(make sure this is with our without trailing slash)
they get http://www.mysite.com/index.php?aff=bob

If http://www.mysite.com/bob/ DOES exist, they get it.

thanks
Avatar of caterham_www
caterham_www
Flag of Germany image

Can /bob/ look like /bob/foo  or /b.o.b./ or /bob147/, too? In other words: Do you expect only characters from a-z followed by an optional slash?
Avatar of bennybutler
bennybutler

ASKER

Hadn't thought much of it, but I'm going to restrict them to a-z, A-Z, 0-9 and dash, underscore.

It should probably accept a trailing slash though as many people are going to type it that way anyway.

if dash and underscore make it excessively difficult I can do without those.

Thanks
No, that's no problem. The only thing is that I'd like to create a RegEx vhich matches not too nuch, because the order of processing is regex of the rewriteRule --> Conditions. So it's better that requests which should not be processed don't run into the conditions (performance)


I don't check for existing files here (!-f), because I expect that all files have an extension (=contain a period).
RewriteEngine on
# is not a directory
RewriteRule %{REQUEST_FILENAME} !-d
# the A-Z is being caught by the NC flag (compiles the regEx case-intensive)
RewriteRule ^([a-z_-]+)/?$ /index.php?aff=$1 [NC,L]

Open in new window

oh, I forgot the numbers so it should read
RewriteEngine on
# is not a directory
RewriteRule %{REQUEST_FILENAME} !-d
# the A-Z is being caught by the NC flag (compiles the regEx case-intensive)
RewriteRule ^([a-z0-9_-]+)/?$ /index.php?aff=$1 [NC,L]

Open in new window

It looks like it's doing the rewrite, but there's a serious side effect...

It's breaking links to all external files (javascript, images, stylesheets)

http://www.stockpublishing.com/asdfasdfafdasdf/

NameVirtualHost 192.168.1.59:80



        DocumentRoot /www/stock/
       
                allow from all
                Options +Indexes

                RewriteEngine on
                # is not a directory
                RewriteRule %{REQUEST_FILENAME} !-d
                # the A-Z is being caught by the NC flag (compiles the regEx case-intensive)
                RewriteRule ^([a-z0-9_-]+)/?$ /index.php?aff=$1 [NC,L]

       
        ServerName stockpublishing.com

ASKER CERTIFIED SOLUTION
Avatar of caterham_www
caterham_www
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
The unintended side effect to this was my 404 page is never triggered because everything matches, but oh well.  I'll just query the db and if I don't get a match to aff=, then I'll redir to the 404.

Thanks!
HEH, btw, I have no idea what all of that means, but it works! Thanks!
You can check this with mod_rewrite as well: Since you have access to the httpd.conf you can use a rewriteMap, an external eg. perl script, which queries the database. You could use php as well, but usually php is compiled not to work for requests on shell.

> then I'll redir to the 404.

but make sure that you send a 404 header (php header function), a redirect/Location header (302/301) to a not found page may irritate search engines.
Avatar of Michael Worsham
You could possibly also try something like this as well...

Options +FollowSymLinks 
 
RewriteEngine On 
 
# If it's not a request to an existing file 
RewriteCond %{SCRIPT_FILENAME} !-f 
# or directory 
RewriteCond %{SCRIPT_FILENAME} !-d 
# rewrite /$1/ to /index.php?aff=$1 
RewriteRule ^([^/]+)/?$ /index.php?aff=$1 [QSA,L]

Open in new window

But his "aff" values don't contain a period, so a RegEx [^/]+ matches more than needed. The problem are not existing files in th filesystem but aff values which does not exist in the database, e.g. /something/
I just realized, it's matching folders that do exist, so even though there is a /join folder, it's still sending me to ?aff=join
Any ideas?
Oh, it's been to late.

Change        RewriteRule /www/stock/$1/ !-d
into

       RewriteCond /www/stock/$1/ !-d

sory.
THAT WAS IT!