Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

Website and Server Administration Forum


You are currently viewing our Website and Server Administration Forum as a guest. Please register to participate.
Login



Reply
Apache .htaccess Rewriting Efficiency
Old 09-10-2008, 08:35 AM Apache .htaccess Rewriting Efficiency
Kiros72's Avatar
Average Talker

Posts: 18
Name: Kiros
Location: Albany, LA - USA
Trades: 0
Part 1

Well, I have two major files for my site: index.php and topics.php. The index.php page doesn't change, so it's not really a concern, however, I have hundreds of pages that are generated through topics.php. My site has certain categories, which have sections, which have pages, so I used to have links like:

/topics.php?category=about&section=legal&page=priva cy

I've used some rewrites to use static links, so that link is now:

/topics/about/legal/privacy

However, I am concerned about the efficiency because I am running on a shared host. Here's the code I used for the rewriting:
Code:
RewriteRule ^topics/([^/]+)/([^/]+)/([^/]+)/?$ /topics.php?category=$1&section=$2&page=$3 [L] 

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /topics\.php\?category=([^&]+)&section=([^&]+)&page=([^\ ]+)\ HTTP/ 
RewriteRule ^topics\.php$ http://www.domain.com/topics/%1/%2/%3? [R=301,L] 

RewriteRule ^topics/([^/]+)/([^/]+)/?$ /topics.php?category=$1&section=$2 [L] 

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /topics\.php\?category=([^&]+)&section=([^\ ]+)\ HTTP/ 
RewriteRule ^topics\.php$ http://www.domain.com/topics/%1/%2? [R=301,L] 

RewriteRule ^topics/([^/]+)/?$ /topics.php?category=$1 [L] 

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /topics\.php\?category=([^\ ]+)\ HTTP/ 
RewriteRule ^topics\.php$ http://www.domain.com/topics/%1? [R=301,L]  

RewriteRule ^topics/?$ /topics.php [L] 

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /topics\.php\ HTTP/ 
RewriteRule ^topics\.php$ http://www.domain.com/topics/? [R=301,L]
I was wondering if I could make it smaller and still get the same results (because every page is not going to have all three variables). If not, might there be a more efficient way of doing this?

Part 2

I can't figure out why Google is sometimes browsing with double slashes (i.e. /forums//search.php), so I put in some code to remove multiple slashes:
Code:
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
I am just as concerned about this piece of code, if not more, than the other code in my .htaccess. I feel that this is incredibly inefficient. The problem is, I want to have a since piece of code that removes various numbers of multiple slashes (i.e. /oldsite//forum///index.php rewriting to /oldsite/forum/index.php). I know that there has to be a better method out there than just using the code I have because that redirects for each additional slash instead of giving a single, clean redirect.

Part 3

I'm not sure if anyone will know this or not... If a RewriteRule uses multiple RewriteCond statements, will Apache stop checking the RewriteCond statements under the first condition that evaluates to false? That would make the most sense, but I'm not sure how the Rewrite Module works

Thanks for taking the time to read! Any help will be greatly appreciated!
Kiros72 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 09-10-2008, 06:47 PM Re: Apache .htaccess Rewriting Efficiency
Ultra Talker

Posts: 251
Location: Belgium, Antwerp, Zoersel
Trades: 0
I think, but you'll have to test it, that the following code does the same as part 1:

Code:
RewriteRule ^topics(?:/([^/]+))?(?:/([^/]+))?(?:/([^/]+))?/?$ /topics.php?category=$1&section=$2&page=$3 [L] 

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /topics\.php\?category=([^&]+)&section=([^&]+)&page=([^\ ]+)\ HTTP/ 
RewriteRule ^topics\.php$ http://www.domain.com/topics/%1/%2/%3? [R=301,L] 

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /topics\.php\?category=([^&]+)&section=([^\ ]+)\ HTTP/ 
RewriteRule ^topics\.php$ http://www.domain.com/topics/%1/%2? [R=301,L] 

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /topics\.php\?category=([^\ ]+)\ HTTP/ 
RewriteRule ^topics\.php$ http://www.domain.com/topics/%1? [R=301,L]  

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /topics\.php\ HTTP/ 
RewriteRule ^topics\.php$ http://www.domain.com/topics/? [R=301,L]
It's only a little shorter of course, and I doubt if it's any faster, but it should work.

Part 2:

Again, only partly a solution:

Code:
RewriteCond %{REQUEST_URI} ^(.*)(/+)(.*)$
RewriteRule . %1/%3 [R=301,L]
Part 3

Most languages do, so probably apache does too. I'm not sure though.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Orodreth is offline
Reply With Quote
View Public Profile Visit Orodreth's homepage!
 
Old 09-11-2008, 03:43 AM Re: Apache .htaccess Rewriting Efficiency
Kiros72's Avatar
Average Talker

Posts: 18
Name: Kiros
Location: Albany, LA - USA
Trades: 0
Thanks for the reply! Unfortunately, I couldn't get the Part 1 code you suggested to work. I get a 500 server error on all the entire site.

As for the code for Part 2, I think that will do a rewrite for every / that appears in the URI, regardless if it has multiple slashes or not... But I couldn't sleep last night, so I did some work and came up with this:
Code:
RewriteCond %{REQUEST_URI} ^/([^/]+)/([/]+)([^/]*/?)$ [OR]
RewriteCond %{REQUEST_URI} ^/[/]+([^/]+)(/)([^/]*/?)$ [OR]
RewriteCond %{REQUEST_URI} ^/[/]+([^/]+)/([/]+)([^/]*/?)$
RewriteRule . %1/%3 [R=301,L]
It seems to work well - very well in fact, but it's only for the first two directories. I say "very well" because it immediately redirects to just one slash instead of just shorting the multiple slashes by one with each redirect. But, until I hear otherwise, I'm not sure that this would be more efficient than previous code. So for the time being, I'm just using this (because the /forums/ directory is the only problem area that I can spot for right now):
Code:
RewriteCond %{REQUEST_URI} ^/forums//(.*)$
RewriteRule ^forums//(.*)$ forums/%1 [R=301,L]
Now for Part 3! That's exactly my take on it lol
Kiros72 is offline
Reply With Quote
View Public Profile
 
Old 09-12-2008, 07:08 AM Re: Apache .htaccess Rewriting Efficiency
Ultra Talker

Posts: 251
Location: Belgium, Antwerp, Zoersel
Trades: 0
Ah, if part 1 doesn't work, then apache probably doesn't support the complete PERL Regular Expression syntax. Try using the folling instead then:

Code:
RewriteRule ^topics(/([^/]+))?(/([^/]+))?(/([^/]+))?/?$ /topics.php?category=$2&section=$4&page=$6 [L] 

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /topics\.php\?category=([^&]+)&section=([^&]+)&page=([^\ ]+)\ HTTP/ 
RewriteRule ^topics\.php$ http://www.domain.com/topics/%1/%2/%3? [R=301,L] 

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /topics\.php\?category=([^&]+)&section=([^\ ]+)\ HTTP/ 
RewriteRule ^topics\.php$ http://www.domain.com/topics/%1/%2? [R=301,L] 

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /topics\.php\?category=([^\ ]+)\ HTTP/ 
RewriteRule ^topics\.php$ http://www.domain.com/topics/%1? [R=301,L]  

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /topics\.php\ HTTP/ 
RewriteRule ^topics\.php$ http://www.domain.com/topics/? [R=301,L]
As for part 2: I'm glad it's working now .
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE

Last edited by Orodreth; 09-13-2008 at 01:19 AM.. Reason: TYPO
Orodreth is offline
Reply With Quote
View Public Profile Visit Orodreth's homepage!
 
Old 09-12-2008, 10:21 AM Re: Apache .htaccess Rewriting Efficiency
Kiros72's Avatar
Average Talker

Posts: 18
Name: Kiros
Location: Albany, LA - USA
Trades: 0
Thanks for the help! Unfortunately, I'm Part 1 is getting a bug. The .htaccess seems to be just fine, but because some pages require the $category (and no other variable), I'm getting my error page. So I know that things are going through correctly, but for example:

/topics/community/ - SHOULD translate into -
/topics.php?category=community

... For it to work correctly. instead:

/topics/community/ - ACTUALLY translates into -
/topics.php?category=community&section=&page=

I guess that wouldn't normally be a problem, but the way that I wrote the core of the site just doesn't allow for that. If $section is set, then a switch will run with the $section, and since nothing is actually there, it goes to the default, which serves a "Can't find stuff" sort of page. The same thing goes for $page (if there is a defined $section).

It looks like I won't be able to shrink the code or improve the performance =/ Oh well. But thanks for the help!

As for Part 2, I came up with another code while I was bored. Do you think that this piece of code:
Code:
RewriteCond %{REQUEST_URI} ^/+([^/]+)//(.*)$
RewriteRule ^[^/]+//.*$ %1/%2 [R=301,L]
... Would be better (more efficient) than this:
Code:
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2
Kiros72 is offline
Reply With Quote
View Public Profile
 
Old 09-13-2008, 01:32 AM Re: Apache .htaccess Rewriting Efficiency
Ultra Talker

Posts: 251
Location: Belgium, Antwerp, Zoersel
Trades: 0
Too bad for part 1 one, but I don't know wany way to shorten it anymore either then.

As for part 2, neither of them strip more than two slashes out in one time. I think the best way would be to use:

Code:
RewriteCond %{REQUEST_URI} ^/+([^/]+)/(/+)(.*)$
RewriteRule . %1/%3
That reduces the amount of redirects needed to strip out all slashes. (Of course, you can still add the [R=301,L] option if you want.

I don't know which version of your code is the fastest though.
__________________

Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
|
Please login or register to view this content. Registration is FREE
Orodreth is offline
Reply With Quote
View Public Profile Visit Orodreth's homepage!
 
Reply     « Reply to Apache .htaccess Rewriting Efficiency
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.24889 seconds with 12 queries