daled Posted February 22, 2009 Share Posted February 22, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/146324-rules-being-ignored/ Share on other sites More sharing options...
corbin Posted February 22, 2009 Share Posted February 22, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/146324-rules-being-ignored/#findComment-768250 Share on other sites More sharing options...
daled Posted February 22, 2009 Author Share Posted February 22, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/146324-rules-being-ignored/#findComment-768457 Share on other sites More sharing options...
corbin Posted February 22, 2009 Share Posted February 22, 2009 Try: RewriteEngine on RewriteCond %{REQUEST_URI} -f RewriteRule (.*) /user.php?user=$1 [L,R,PT] Rewriting anything to user.php?user=$1 will cause an infinite loop because it will rewrite user.php=user to user.php=user.php And so on.... Quote Link to comment https://forums.phpfreaks.com/topic/146324-rules-being-ignored/#findComment-768502 Share on other sites More sharing options...
daled Posted February 22, 2009 Author Share Posted February 22, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/146324-rules-being-ignored/#findComment-768513 Share on other sites More sharing options...
corbin Posted February 23, 2009 Share Posted February 23, 2009 Oh yeah -f should've been !-f. Sorry 'bout that. Glad you caught it lol. Try: RewriteEngine on RewriteCond %{REQUEST_URI} !-f RewriteRule (.*) /user.php?user=$1 [L,R,PT] Quote Link to comment https://forums.phpfreaks.com/topic/146324-rules-being-ignored/#findComment-768876 Share on other sites More sharing options...
daled Posted February 23, 2009 Author Share Posted February 23, 2009 Still no success. This script results in a 400 - bad request. Quote Link to comment https://forums.phpfreaks.com/topic/146324-rules-being-ignored/#findComment-769151 Share on other sites More sharing options...
corbin Posted February 24, 2009 Share Posted February 24, 2009 Try changing [L,R,PT] to [L] (unless you're sure you want to send a 302 header and use PT). Besides that, I don't know what it could be. I'll test it locally in a sec. Quote Link to comment https://forums.phpfreaks.com/topic/146324-rules-being-ignored/#findComment-769727 Share on other sites More sharing options...
daled Posted February 24, 2009 Author Share Posted February 24, 2009 YES!!! I believe that did it!!! hopefully it works everywhere else I need to use it!! Thank you so much! Quote Link to comment https://forums.phpfreaks.com/topic/146324-rules-being-ignored/#findComment-770034 Share on other sites More sharing options...
daled Posted February 24, 2009 Author Share Posted February 24, 2009 Wait, but now every URL is being redirected, even one's that are files... RewriteEngine on RewriteCond %{REQUEST_URI} !-f RewriteRule ^(.*) user.php?user=$1 [L] Quote Link to comment https://forums.phpfreaks.com/topic/146324-rules-being-ignored/#findComment-770035 Share on other sites More sharing options...
corbin Posted February 24, 2009 Share Posted February 24, 2009 Oooo hrmmm.... maybe request URI was the wrong thing hehe ;p. RewriteCond %{SCRIPT_FILENAME} !-f Quote Link to comment https://forums.phpfreaks.com/topic/146324-rules-being-ignored/#findComment-770573 Share on other sites More sharing options...
daled Posted February 25, 2009 Author Share Posted February 25, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/146324-rules-being-ignored/#findComment-770751 Share on other sites More sharing options...
corbin Posted February 25, 2009 Share Posted February 25, 2009 ^$ 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. Quote Link to comment https://forums.phpfreaks.com/topic/146324-rules-being-ignored/#findComment-770781 Share on other sites More sharing options...
daled Posted February 25, 2009 Author Share Posted February 25, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/146324-rules-being-ignored/#findComment-770989 Share on other sites More sharing options...
corbin Posted February 26, 2009 Share Posted February 26, 2009 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). Quote Link to comment https://forums.phpfreaks.com/topic/146324-rules-being-ignored/#findComment-771525 Share on other sites More sharing options...
daled Posted February 26, 2009 Author Share Posted February 26, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/146324-rules-being-ignored/#findComment-771753 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.