zq29 Posted February 24, 2008 Share Posted February 24, 2008 Here is an extract from an .htaccess file I'm working on: RewriteRule ^register/([a-z]+)\.html$ index.php?page=register&type=$1 RewriteRule ^([a-z_-]+)\.html$ index.php?page=$1 NOTE: I have a file called register.php in my doc root. Ok, so when I point my browser at localhost/register.html I get the contents of register.php loaded into my index.php file. If I point my browser to localhost/register/foo.html I get the contents of register.php without it being loaded into index.php and 'foo' is not processed. If I change my .htaccess to the following: RewriteRule ^registration/([a-z]+)\.html$ index.php?page=register&type=$1 RewriteRule ^([a-z_-]+)\.html$ index.php?page=$1 Pointing my browser to localhost/registration/foo.html displays the contents of register.php inside index.php with 'foo' being processed. Can anyone explain why the first rule doesn't work as expected? Do the rules conflict in some way? Quote Link to comment https://forums.phpfreaks.com/topic/92728-odd-mod_rewrite-problem/ Share on other sites More sharing options...
448191 Posted February 24, 2008 Share Posted February 24, 2008 The only thing that doesn't make sense to me is localhost/register/foo.html being rewritten to register.php. That is what you are saying, no? Quote Link to comment https://forums.phpfreaks.com/topic/92728-odd-mod_rewrite-problem/#findComment-475128 Share on other sites More sharing options...
zq29 Posted February 24, 2008 Author Share Posted February 24, 2008 The only thing that doesn't make sense to me is localhost/register/foo.html being rewritten to register.php. That is what you are saying, no? Correct, it should be rewritten to index.php?page=register&type=foo - But it's not, it's being rewritten to register.php. None of my rules rewrite a page to not include "index.php?page=" which is why I'm confused... Quote Link to comment https://forums.phpfreaks.com/topic/92728-odd-mod_rewrite-problem/#findComment-475141 Share on other sites More sharing options...
Daniel0 Posted February 24, 2008 Share Posted February 24, 2008 I did some tests and it works as expected for me. What's the rest of your .htaccess file? What GET variables are set when register.php is called through one of the rewritten URLs? Quote Link to comment https://forums.phpfreaks.com/topic/92728-odd-mod_rewrite-problem/#findComment-475145 Share on other sites More sharing options...
zq29 Posted February 27, 2008 Author Share Posted February 27, 2008 Sorry for the late reply guys, been a bit busy! Anyways, I have just conducted a fresh test. Here are my three files: .htaccess RewriteEngine On RewriteRule ^([a-z_]*)\.html$ index.php?page=$1 RewriteRule ^foo/([0-9]+)$ index.php?page=foo&n=$1 index.php <h1>index.php</h1> <?php if(isset($_GET['page'])) include($_GET['page'].".php"); ?> foo.php <h1>foo.php</h1> <?=(isset($_GET['n'])) ? "NUMBER - $_GET[n]" : "";?> URL: http://localhost OUTPUT: index.php URL: http://localhost/foo.html OUTPUT: index.php foo.php URL: http://localhost/foo/4 OUTPUT: foo.php URL: http://localhost/foo.php?n=4 OUTPUT: foo.php NUMBER - 4 My problem is, viewing http://localhost/foo/4 should give the same output as http://localhost/foo.php?n=4. Does this work as expected on your set-up? Maybe there is a problem with my machines config somewhere? Quote Link to comment https://forums.phpfreaks.com/topic/92728-odd-mod_rewrite-problem/#findComment-478284 Share on other sites More sharing options...
fenway Posted February 27, 2008 Share Posted February 27, 2008 You don't have any [L] there. Quote Link to comment https://forums.phpfreaks.com/topic/92728-odd-mod_rewrite-problem/#findComment-478287 Share on other sites More sharing options...
zq29 Posted February 27, 2008 Author Share Posted February 27, 2008 You don't have any [L] there. 'last|L' (last rule) Stop the rewriting process here and don't apply any more rewriting rules. This corresponds to the Perl last command or the break command from the C language. Use this flag to prevent the currently rewritten URL from being rewritten further by following rules. For example, use it to rewrite the root-path URL ('/') to a real one, e.g., '/e/www/'. Sounds like that could be useful, thanks for pointing that out Fenway - Will give it a try tomorrow... Quote Link to comment https://forums.phpfreaks.com/topic/92728-odd-mod_rewrite-problem/#findComment-478397 Share on other sites More sharing options...
zq29 Posted February 28, 2008 Author Share Posted February 28, 2008 .htaccess RewriteEngine On RewriteRule ^foo/([0-9]+)$ index.php?page=foo&n=$1 [L] RewriteRule ^([a-z_]*)\.html$ index.php?page=$1 No change... EDIT: Out of curiosity, I dropped the second line of that .htaccess file and it's still doing the same thing. Could this be a config issue? FURTHER EDIT: Just been messing around with my VirtualHosts config, here it is: <VirtualHost *> ServerName test.kris ServerAlias test.kris ServerAdmin kris@localhost DocumentRoot /var/www/test <Directory /var/www/test/> Options Indexes FollowSymLinks MultiViews AllowOverride all Order allow,deny allow from all </Directory> </VirtualHost> Dropping out the "MultiViews" bit and restarting Apache appears to have fixed my issue, but the Apache docs weren't that helpful in explaining what "MultiViews" does... Quote Link to comment https://forums.phpfreaks.com/topic/92728-odd-mod_rewrite-problem/#findComment-479234 Share on other sites More sharing options...
fenway Posted February 28, 2008 Share Posted February 28, 2008 Strange. Quote Link to comment https://forums.phpfreaks.com/topic/92728-odd-mod_rewrite-problem/#findComment-479257 Share on other sites More sharing options...
448191 Posted February 28, 2008 Share Posted February 28, 2008 From what I could gather, MultiViews allows you to omit the file extension. It makes sense that this would interfere with your mod_rewrite rules. I also found this blogpost, which explains why you should steer clear from it: http://www.gerd-riesselmann.net/archives/2005/04/beware-of-apaches-multiviews Bottom line: you don't need it. Quote Link to comment https://forums.phpfreaks.com/topic/92728-odd-mod_rewrite-problem/#findComment-479329 Share on other sites More sharing options...
zq29 Posted February 28, 2008 Author Share Posted February 28, 2008 From what I could gather, MultiViews allows you to omit the file extension. It makes sense that this would interfere with your mod_rewrite rules. I also found this blogpost, which explains why you should steer clear from it: http://www.gerd-riesselmann.net/archives/2005/04/beware-of-apaches-multiviews Bottom line: you don't need it. Thanks for the link John, informative read. Quote Link to comment https://forums.phpfreaks.com/topic/92728-odd-mod_rewrite-problem/#findComment-479374 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.