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
Apache Web ServerLinux Networking

Avatar of undefined
Last Comment
bennybutler

8/22/2022 - Mon
caterham_www

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?
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
caterham_www

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

Experts Exchange has (a) saved my job multiple times, (b) saved me hours, days, and even weeks of work, and often (c) makes me look like a superhero! This place is MAGIC!
Walt Forbes
caterham_www

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

bennybutler

ASKER
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
caterham_www

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
bennybutler

ASKER
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!
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
bennybutler

ASKER
HEH, btw, I have no idea what all of that means, but it works! Thanks!
caterham_www

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.
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

Your help has saved me hundreds of hours of internet surfing.
fblack61
caterham_www

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/
bennybutler

ASKER
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?
caterham_www

Oh, it's been to late.

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

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

sory.
⚡ FREE TRIAL OFFER
Try out a week of full access for free.
Find out why thousands trust the EE community with their toughest problems.
bennybutler

ASKER
THAT WAS IT!