Jump to content

Rules being ignored


daled

Recommended Posts

I'm using the mod_rewrite module to make some better looking URLs on my website. Here's my script within the .htaccess file:

 

RewriteEngine on

RewriteCond %{HTTP_HOST} ^users$

RewriteRule ^/(.*)$ user.php?user=$1 [L,R,PT]

 

What this is suppose to do is check to see if the host contains the text 'users' (users.domain.com). Then find what ever comes after the domain (users.domain.com/text-to-match) and then transform the url to www.domain.com/users.php?user=text-to-match

 

My DNS resolves users.domain.com to the IP address of my server. But I don't believe that this should be a problem because the script should still execute. In any case, the URL is not being rewritten and my index page shows instead

 

I ran phpinfo() on my server and it doesn't seem that mod_rewrite appears on the page under additional modules. I believe that I have configured it correctly: uncommented the LoadModule statement in httpd.conf.

 

Also simpler redirects work; like redirecting news.php to register.php. But when I try to redirect a psuedo-directory, it doesn't work.

 

I am running a WAMP server.

Link to comment
Share on other sites

RewriteRule does nothing on the domain level.

 

 

That basically says:

 

Rewriting is turned on

If the host name starts with users

Rewrite anything starting with / to users.php?user=$1

 

 

 

Try changing it to:

 

RewriteRule (.*) user.php?user=$1 [L,R,PT]

 

 

That will rewrite user.php to user.php?user=user.php though....  So you will want to do some other checking.

 

 

An easy solution would be:

 

RewriteCond %{REQUEST_URI} -f

RewriteRule (.*) user.php?user=$1 [L,R,PT]

 

 

That would only rewrite things that are not files.

Link to comment
Share on other sites

I changed the script to:

 

RewriteEngine on

RewriteRule ^(.*)$ user.php?user=$1 [L,R,PT]

 

after:

 

RewriteEngine on

RewriteCond %{REQUEST_URI} -f

RewriteRule (.*) user.php?user=$1 [L,R,PT]

 

didn't work.  Now I'm getting a 400 error and the rule is still being ignored.

Link to comment
Share on other sites

RewriteEngine on

RewriteCond %{REQUEST_URI} !-f

RewriteRule (.*) /user.php?user=$1 [L,R,PT]

(Results in 400 - bad request)

 

and

 

RewriteEngine on

RewriteCond %{REQUEST_URI} -f

RewriteRule (.*) /user.php?user=$1 [L,R,PT]

(Results in 404 - not found)

 

both prove unsuccessful.  i'm now starting to doubt whether mod_rewrite is actually enabled.  was there anything i had to do besides uncommenting the line in httpd.conf?

Link to comment
Share on other sites

Great! That solved my problem.  But in doing so it created another.  I added some script that redirects '/forums' to forums.php and '/news' to news.php etc.  The problem arises when I go to the homepage that has no REQUEST_URI/SCRIPT_FILENAME.  I get a 500 - server error, which is expected because there's no REQUEST_URI/SCRIPT_FILENAME.  So how can I check for an empty string in regexp?

Link to comment
Share on other sites

^$ is an empty string.  ^ means start and $ means end, so that would essentially means it starts then there is nothing, and then the string ends.

 

 

 

If you put your static rules first with the [L] flag, the RewriteCond and RewriteRule from this thread should never be reached though.

Link to comment
Share on other sites

So I fixed that by adding a separate cond and rule for each case.  But as I try to expand my urls to something like /about/administration (with two pseudo directories) I run into more trouble.

 

The script:

 

RewriteCond %{SCRIPT_FILENAME} about/[A-Za-z]+

RewriteRule ^/about/([A-Za-z]+) /about.php?action=$1 [L]

 

So that's supposed to match "about/text" and convert it to about.php?action=text.  It only half works.  It redirects the page, but doesn't redirect with the correct action variable.  It just completely skips over it.  And it seems that when it loads the page, it loads into a sub-directory so none of the CSS or images load.  To see what I'm talking about: http://www.fmxstudio.com/about/administration

 

Thank you for all your help, corbin.

Link to comment
Share on other sites

RewriteCond %{SCRIPT_FILENAME} about/[A-Za-z]+

RewriteRule ^/about/([A-Za-z]+) /about.php?action=$1 [L]

 

 

Having both of those lines doesn't makes sense.  The first line basically means if the filename contains about/(letters), then do the rewrite.  So the cond is essentially redundant.

 

 

(It should be ^about by the way if you're trying to make sure it starts with about.)

 

 

As for the rewrite rule, the first / isn't included, so it should be:

 

RewriteRule ^about/..........

 

 

"And it seems that when it loads the page, it loads into a sub-directory so none of the CSS or images load."

 

 

If you're using relative paths, the browser will request them relative to the current perceived location.  The browser has no idea that it's actually seeing /about.php.  As far as it knows, it really is seeing /about/blah.

 

So, you will either need to rewrite resources or use absolute paths.  I suggest absolute paths since they are better for caching and easier to manage (in my opinion).

Link to comment
Share on other sites

The absolute paths are working.  Thanks so much.  But my script is still having trouble backtracking to the url.

 

RewriteRule ^about/([A-Za-z]+) about.php?action=$1 [L]

 

This doesn't find the string after about/ or it is but it's not getting put into the new url.

 

:edit:

Op. Fixed that.  That had to be above some other script that was also searching for about.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.