multiple conditions or defined variables

Discuss practical ways rearrange URLs using mod_rewrite.

multiple conditions or defined variables

Postby foyleman » Wed Oct 28, 2009 7:18 am

I'm trying to simplify some rewrite code and hope that the place I always use for reference can help me.

The code I have is setup to redirect pages in a CMS. Here's the code I am using:
Code: Select all
RewriteRule (^.*)(BBCode|Downloads|Members|Messages|News|Privacy|Search|Sections|Static)-?([0-9A-Za-z]*)-?([0-9A-Za-z]*)-?([0-9A-Za-z]*).html   index.php?mod=$2&op=$3&id=$4&start=$5&%{QUERY_STRING}   [L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ (.*)/index\.php\?mod=(BBCode|Downloads|Members|Messages|News|Privacy|Search|Sections|Static)&op=([^&]+)&id=([^&]+)&start=([^&]+)&?(.*)\ HTTP/
RewriteRule ^index\.php$ %1/%2-%3-%4-%5.html?%6 [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ (.*)/index\.php\?mod=(BBCode|Downloads|Members|Messages|News|Privacy|Search|Sections|Static)&op=([^&]+)&id=([^&]+)&?(.*)\ HTTP/
RewriteRule ^index\.php$ %1/%2-%3-%4.html?%5 [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ (.*)/index\.php\?mod=(BBCode|Downloads|Members|Messages|News|Privacy|Search|Sections|Static)&op=([^&]+)&?(.*)\ HTTP/
RewriteRule ^index\.php$ %1/%2-%3.html?%4 [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ (.*)/index\.php\?mod=(BBCode|Downloads|Members|Messages|News|Privacy|Search|Sections|Static)&?(.*)\ HTTP/
RewriteRule ^index\.php$ %1/%2.html?%3 [R=301,L]

The first line is a URL shortner so that a page such as Sections-read-1.html will display the content for index.php?mod=Sections&op=read&id=1.

When a module (mod= and then the string of OR statements) is added or removed from the CMS, a script modifies the code above within the htacces file. I would like to make it as easy as possible to modify. Therefore, the next 4 sets of lines is what I would really like to condense. Each set is a condition for a slightly shorter url. In example:
  • index.php?mod=Sections&op=read&id=1&start=20 redirects to Sections-read-1-20.html
  • index.php?mod=Sections&op=read&id=1 redirects to Sections-read-1.html
  • index.php?mod=Sections&op=read redirects to Sections-read.html
  • index.php?mod=Sections redirects to Sections.html
This leads me to two questions:
  1. Can I condense these sets of conditions into a simpler else-if statement?
  2. Can I define a custom variable at the start of the .htaccess file that represents the following string?
    BBCode|Downloads|Members|Messages|News|Privacy|Search|Sections|Static
foyleman
 
Posts: 5
Joined: Wed Oct 28, 2009 6:55 am

Postby richardk » Wed Oct 28, 2009 11:33 am

Can I condense these sets of conditions into a simpler else-if statement?

At the moment i can't think of a way to make it shorter.

I suggest you either redirect in index.php or rewrite to a different PHP file and do the redirects with PHP because currently your rules won't work if the parameters aren't in the right order and the rules will get more complicated to match any order.

Can I define a custom variable at the start of the .htaccess file that represents the following string?
BBCode|Downloads|Members|Messages|News|Privacy|Search|Sections|Static

No, you can define variables but you can't use them in regular expressions.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby foyleman » Thu Oct 29, 2009 8:45 am

Thanks richardk.

Anyone have an idea why this would work on my windows installation of xampp but does not work on my Linux based web server? modrewrite does work in general, however this particular code loops and doesn't resolve.

I'm having trouble figuring out why it works on one machine and not the other.
I've broken it down to simple lines in the hopes of figuring this out:
Code: Select all
RewriteRule ^Sections-read-1.html   index.php?mod=Sections&op=read&id=1   [L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ (.*)/index\.php\?mod=Sections&op=read&id=1\ HTTP/
RewriteRule ^index.php$ /Sections-read-1.html? [R=301,L]
foyleman
 
Posts: 5
Joined: Wed Oct 28, 2009 6:55 am

Postby richardk » Fri Oct 30, 2009 11:14 am

I can't see anything obvious that would stop it working, try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^(.*&)?mod=Sections(&.*)?$ [NC]
RewriteCond %{QUERY_STRING} ^(.*&)?op=read(&.*)?$ [NC]
RewriteCond %{QUERY_STRING} ^(.*&)?id=1(&.*)?$ [NC]
RewriteRule ^(index\.php)?$ /Sections-read-1.html? [R=301,L]

RewriteRule ^Sections-read-1\.html$ /index.php?mod=Sections&op=read&id=1 [L]

instead.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby foyleman » Fri Oct 30, 2009 12:07 pm

Nope. That doesn't work.
Going from Sections-read-1.html does display the content for index.php?mod=Sections&op=read&id=1

BUT from index.php?mod=Sections&op=read&id=1 I get an endless loop.

Perhaps it's trying to append to the Sections-read-1.html and trying to resolve: Sections-read-1.html?mod=Sections&op=read&id=1 which I could see never quite resolving. Although I don't know how to stop that if it is indeed the case. The URL doesn't supply me with anything I can use to debug the situation.

I still find it amazing that this works on my xampp installation and not on my linux server.
foyleman
 
Posts: 5
Joined: Wed Oct 28, 2009 6:55 am

Postby richardk » Mon Nov 02, 2009 10:05 am

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} !^(.*&)?dont=redirect(&.*)?$ [NC]
RewriteCond %{QUERY_STRING} ^(.*&)?mod=Sections(&.*)?$ [NC]
RewriteCond %{QUERY_STRING} ^(.*&)?op=read(&.*)?$ [NC]
RewriteCond %{QUERY_STRING} ^(.*&)?id=1(&.*)?$ [NC]
RewriteRule ^(index\.php)?$ /Sections-read-1.html? [R=301,L]

RewriteRule ^Sections-read-1\.html$ /index.php?mod=Sections&op=read&id=1&dont=redirect [L]

It's a bit of a hack.

The URL doesn't supply me with anything I can use to debug the situation.

And i take it you don't have access to the server configuration (not a .htaccess file) to find define a RewriteLog?

I still find it amazing that this works on my xampp installation and not on my linux server.

What versions of Apache are they?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby foyleman » Mon Nov 02, 2009 10:40 am

We got a winner! Apparently the issue is with Apache.

xampp: Apache/2.2.11 (Win32) DAV/2 mod_ssl/2.2.11 OpenSSL/0.9.8i mod_autoindex_color PHP/5.2.8 mod_perl/2.0.4 Perl/v5.10.0

linux server: Apache/1.3.37 (Unix) mod_auth_passthrough/1.8 mod_log_bytes/1.2 mod_bwlimited/1.4 FrontPage/5.0.2.2635.SR1.2 mod_ssl/2.8.28 OpenSSL/0.9.7a

I ran a test on another server running Apache/2.0.46 and my original code worked.

Therefore I am going to take that to mean the version of Apache is at fault here.

Thank you richardk. I hope this thread helps someone else in the future.
foyleman
 
Posts: 5
Joined: Wed Oct 28, 2009 6:55 am

Postby richardk » Mon Nov 02, 2009 10:56 am

I don't have the problem with Apache 1.3.37 on WinXP. I would think it is more likely to be a configuration difference or some variables set incorrectly (or not set at all) (ENV:REDIRECT_STATUS and THE_REQUEST).
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby foyleman » Mon Nov 02, 2009 11:22 am

Well, to answer the other questions...

The new code you provided did not resolve the issue.
  • Sections-read-1.html displays the correct information.
  • index.php?mod=Sections&op=read&id=7 redirects to index.php?mod=Sections&op=read&id=7&dont=redirect
I do have root access to the server but did not know I could create an htaccess log. Do you have a link or other information on how I can set this up?

In PHP _SERVER["REQUEST_URI"] does output correctly. I don't know how to test for Apache and the REDIRECT_STATUS.
foyleman
 
Posts: 5
Joined: Wed Oct 28, 2009 6:55 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: Google [Bot] and 26 guests

cron