Jump to content

[SOLVED] Only one RewriteRule working


themistral

Recommended Posts

Hi guys,

 

I am having a problem in that only the first rule is working.

If I comment out the first rule, the second one works fine.

Any ideas what might solve it?

 

Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\.html$ index.php?page=$1 [L]
RewriteRule ^(.*)-(.*)\.html$ shows.php?id=$1&name=$2 [L]

Link to comment
Share on other sites

If you intended to chain them then you've marked both rules as the last rule without additional RewriteCond statements

If not then the first rule matching means the 2nd won't match after rewrite since the 1st rewrites to index.php and the 2nd matches to *.html

 

Have you tried enabling logging and watching the rewrite process using tail -f ??

Link to comment
Share on other sites

I have taken out the [L] after the first rule, but it's still not working correctly.

 

I am still new to this so just to check, my understanding is that the first rule applies to the index.php page only and the second to shows.php.

 

I am trying to sort my server out for error logging - currently getting an internal server error  ???

 

I have used

RewriteLog "C:\my\file\path\rewrite.log"
RewriteLogLevel 9

 

I am running this on a local machine so I can mess around with server configs easily if pointed in the right direction!

Link to comment
Share on other sites

Since you can have more than one rule in your config each block of rules (up to the [L] - Last) may or may not apply for a given clause or condition

 

Once rewritten subsequent rules are NOT ignored altogether or "skipped" as with a programming language script. Obviously if that were the case you could only have one rule in your config files!.

 

Once a URL is rewritten subsequent rules are checked against the URL to see if they also apply

The only way to really understand what is happening is to enable debug logging and watch this stuff in real time using "tail"

Some particularly unexpected things often happen with rules - the only way to be sure is check the logs.

 

    RewriteEngine On
    RewriteLogLevel 9
    RewriteLog logs/rewrite.log

 

If you posted exactly what you were trying to do someone might be able to script you a ruleset.

Link to comment
Share on other sites

This is my full code - I use the AddType command to parse php through html files.

 

AddType application/x-httpd-php .php .htm .html

Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\.html$ index.php?page=$1 
RewriteRule ^(.*)-(.*)-by-(.*)\.html$ shows.php?showid=$1&showname=$2&writer=$3 [L]

 

I know that both rules work, it's just that the 2nd one won't run unless I comment the 1st one out - which makes me think I must have an error in my syntax somewhere!

 

What I am trying to achieve is:

Rule1

index.php has a query string containing 1 pairing - the value of page should become the new URL with .html added to the end.

Rule2

shows.php has a query string containing 3 pairings - the string I want to achieve is showid-showname-by-writer.html.

 

Like I said, rule 2 works if rule 1 is commented out.

 

Still can't seem to get the error reporting working  >:(

Link to comment
Share on other sites

It's just one of those things about rewriting that may not immediately be obvious. Subsequent rewrites, circular refs: all part of the joys of Mod_rewrite. Rewriting one rule does not mean others may not apply, nor that they may not apply with the rewritten reference in ways which you didn't expect.

 

As I said - the only way to be sure is to enable logging and see whether what is *actually* happening matches your assumptions ;)

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.