Jump to content

Odd mod_rewrite problem


zq29

Recommended Posts

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

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.