Apache Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsSystem AdministrationApache Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old October 17th, 2000, 04:25 PM
forefront forefront is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2000
Posts: 165 forefront User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 55 m 52 sec
Reputation Power: 9
The previous rewwrite rules on mod_rewrite given at http://www.devshed.com/Talk/Forums/...TML/000083.html
have been very helpful. I need help with two more if you don't mind, I cannot get my head round regex!

RULE 1:

1. Our site has some existing .shtml .html and .txt pages and three directories (images, css, data) that we will retain for total backward compatibility.

2. The new system will pass variables to a PHP script (the PHP script will explode the URL, so parameters such as "id" do not need including in the rewrite rule) The PHP script will also convert to lowercase any "MissTypEd" URLs.

3. We want one index.php page in the /htdocs/ root that will handle all client requests except those as mentioned in 1.

4. URLs will be in the format:
http://www.oursite.com/category/ OR http://www.oursite.com/category http://www.oursite.com/category/articleid/ OR http://www.oursite.com/category/articleid

e.g.: http://www.oursite.com/news/3/ http://www.oursite.com/views/21 http://www.oursite.com/foo/22/ http://www.oursite.com/oink/13

etc...

The CAsE of the category doesn't matter as the PHP script will convert the string to lowercase.

Finally (if this can be done with rewrite rules) we want error pages (404, 403, 401, 500, etc.,) to be called using the standard error pages the server would throw without our system.

RULE 2:
Same as rule 1 except point 2 requires mod_rerite to pass the variables to the PHP script as oursite.com/category and oursite.com/category/id which PHP will then parse.

Many thanks for your help


David

Reply With Quote
  #2  
Old October 17th, 2000, 05:04 PM
freebsd
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
>>the PHP script will explode the URL, so parameters such as "id"
>>do not need including in the rewrite rule

Hey, I thought my previous posts already cover everything for your so-called new system.

Without the "id":

Example for www.mysite.com/3/ ############################################
RewriteEngine on
RewriteRule ^(.+)/$ index.php?$1 [T=application/x-httpd-php]
############################################

RewriteRule ^([A-Za-z0-9_]+)([^/A-Za-z0-9_]+)/?$ $1/$2/
RewriteRule ^([^/]+)/?(.*)/$ index.php?category=${lc:$1}&id=${lc:$2} [T=application/x-httpd-php,L]

If you don't want the "category", simply remove it as:
RewriteRule ^([^/]+)/?(.*)/$ index.php?${lc:$1}&id=${lc:$2} [T=application/x-httpd-php,L]

The only thing the previous examples didn't cover is the error pages.

>>we want error pages to be called using the standard
>>error pages the server would throw without our system.

I DON'T think you can use standard error pages any longer.
1) All your relative URLs are no longer a file system on the server
2) They are handled directly by mod_rewrite first, then to your index.php script to determine the output, so ErrorDocument fits nowhere.

If you insist to use ErrorDocument to handle that, then ALL REQUEST_URI will lead you to 404 page since they don't exist at all. Your ONLY option is to let your PHP script to handle that matter and redirect to the appropriate error page.

Reply With Quote
  #3  
Old October 18th, 2000, 01:48 AM
forefront forefront is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2000
Posts: 165 forefront User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 55 m 52 sec
Reputation Power: 9
>>>Hey, I thought my previous posts already cover everything for your so-called new system.

I apologise for any apparent repetition, your help is appreciate. I more wanted to ensure all avenues were covered - yor probably did cover all but I have some fun interpreting what the rewrite rules/regex mean!

Thank you

David

Reply With Quote
  #4  
Old October 18th, 2000, 05:38 PM
forefront forefront is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2000
Posts: 165 forefront User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 55 m 52 sec
Reputation Power: 9
I have this in .htaccess:

RewriteEngine on
RewriteRule ^([A-Za-z0-9_]+)([^/A-Za-z0-9_]+)/?$ $1/$2/
RewriteRule ^([^/]+)/?(.*)/$ index.php?category=$1&id=$2 [T=application/x-httpd-php,L]

and index.php is this:

<?PHP
echo "$category $id";
?>

The result (of echo) when typing mysite/category/id/ is:

categorycategory/id/id=id

I just want the two variables "category" and "id" on their own, so I can manipulate them and for the trailing slahs on "category" and "id" to not matter whether it is there or not. Forget the conversion of upper/lowercase URLs,

What am I doing wrong to get the odd results I get?

Thanks

Reply With Quote
  #5  
Old October 18th, 2000, 05:52 PM
freebsd
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
>>The result (of echo) when typing mysite/category/id/ is:
>>categorycategory/id/id=id

Can you give a real example instead of using the same category name and id name?
Say "mysite/foo/3/", what echo result do you get?

Reply With Quote
  #6  
Old October 18th, 2000, 06:01 PM
forefront forefront is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2000
Posts: 165 forefront User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 55 m 52 sec
Reputation Power: 9
Using that exact example I get:

foofoo/3/id=3

and when I miss trailing slash I get a 404.

David

[This message has been edited by forefront (edited October 18, 2000).]

Reply With Quote
  #7  
Old October 18th, 2000, 07:15 PM
freebsd
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
>>Using that exact example I get:
>>foofoo/3/id=3

That's weird!! Maybe a misspell somewhere?

I have just tested it with the following:
#############################################
#http://www.mydomain.com/.htaccess
RewriteEngine on
RewriteRule ^([A-Za-z0-9_]+)([^/A-Za-z0-9_]+)/?$ $1/$2/
RewriteRule ^([^/]+)/?(.*)/$ index.php?category=$1&id=$2 [T=application/x-httpd-php,L]

#http://www.mydomain.com/index.php
<?php
echo "$category $id';
?>
#############################################

When typing http://www.mydomain.com/foo/3/, I get the exact output "foo 3" as the echo output.
When typing http://www.mydomain.com/foo/3, I get "foo 3" as the echo output, however, the URL in the browser location bar has changed to http://www.mydomain.com/foo/3/ automatically because my RewriteRule $1/$2/ adds the trailing slash ONLY if it's missing.

Remember the "odd_dir" and "another_odd_dir" from the 000083.html page?
For my test a while ago, when I type http://www.mydomain.com/jsp/3/, I also get the output as "jsp 3". But when trying http://www.mydomain.com/jsp/3 (without trailing slash), this time, the output is "404". The reason is that, http://www.mydomain.com/jsp/ actually exists (as a directory in the file system) while my http://www.mydomain.com/.htaccess hasn't have the "odd_dir" to EXCLUDE "jsp" dir.

Whoever reading this, please help forefront (David) to try this out.
Just two files:
the .htaccess and index.php exactly as shown above.


Reply With Quote
  #8  
Old October 18th, 2000, 07:32 PM
forefront forefront is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2000
Posts: 165 forefront User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 55 m 52 sec
Reputation Power: 9
(hardly daring to answer...)
http://www.landofthesun.org/index.php?category=foo&id=3

produces:

foo 3

But:
http://www.landofthesun.org/foo/3/

produces:

foofoo/3/id=3

(you can check the .htaccess file at that URL and that and the PHP script I copied and pasted from your code above.

Web server is Apache O/S BSDi 4.1 (vservers)...headache is at the front!

David

Reply With Quote
  #9  
Old October 18th, 2000, 08:39 PM
freebsd
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
Okay, try this one:

RewriteEngine on
RewriteRule ^([A-Za-z0-9_]+)/([A-Za-z0-9_]+)/?$ index.php?category=$1&id=$2 [T=application/x-httpd-php,L]

It now merged both rewrite rules to one.
Note the ending /?$, so /foo/3 or /foo/3/ conditions should match.

When you are really done with the mod_rewrite stuffs, you should move everything from your .htaccess to httpd.conf within the <Directory "/path/to/htdocs"> so it loads faster.

I don't know why the original one didn't work out for you but it worked fine for me. Anyway, at the 2nd [A-Za-z0-9_], you now should see I removed the / slash from the [], so a request /foo (without /3) or /foo/ won't work any longer. It has to be a real category variable and an id variable. Instead of putting both into one rewriterule, let's make it two rules, whichever (just category or category with id occurs more often, put it above the other.

i.e.
RewriteEngine on
RewriteRule ^([A-Za-z0-9_]+)/?$ index.php?category=$1 [T=application/x-httpd-php,L]
RewriteRule ^([A-Za-z0-9_]+)/([A-Za-z0-9_]+)/?$ index.php?category=$1&id=$2 [T=application/x-httpd-php,L]

Guess what? With the above two rules, you now don't need to set any odd_dirs nor shtml rules. The catch is, if a file or dir really exists in the file system, it will bypass the rewriterule completely.
The order:
1) Determine if it's a file system
2) Follow rewrite rules
3) Send to ErrorDocument 404 for improper format of request such as: /foo/3/bar/ or /foo/3/bar.

For a proper request format such as: /foo/3/ or /foo/3, you can't use the standard ErrorDocument, your index.php MUST be the one to handle that.

For id part, adjust [A-Za-z0-9_] to something like [0-9] would reduce the load to your index.php when handling ErrorDocument.

[This message has been edited by freebsd (edited October 18, 2000).]

Reply With Quote
  #10  
Old October 19th, 2000, 03:22 AM
forefront forefront is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2000
Posts: 165 forefront User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 55 m 52 sec
Reputation Power: 9
Thank you for your continued patience with this one. I really appreciate it.

OK, using this rule:

RewriteEngine on
RewriteRule ^([A-Za-z0-9_]+)/?$ index.php?category=$1 [T=application/x-httpd-php,L]
RewriteRule ^([A-Za-z0-9_]+)/([A-Za-z0-9_]+)/?$ index.php?category=$1&id=$2 [T=application/x-httpd-php,L]

This: http://www.landofthesun.org/foo
OR: http://www.landofthesun.org/foo/

gives:

"foo" as the output

This: http://www.landofthesun.org/foo/6
GIVES
foofoo/6id=6

and this: http://www.landofthesun.org/foo/6/
gives:
foofoo/6/id=6

(including an extra slash)

Can mod_rewrite function differently on different servers/setups? Can it be a PHP bug, though the script is so simple I cannot see how.

I am really scratching my head now!

I confirm again that I am just copying and pasting the stuff you give, at a UNIX command prompt using pico or vi as the editor

It appears one variable can be passed (i.e. change the single rule match to "id" rather than "category") but it is the twin one that foobars

Wot a pig!

David

[This message has been edited by forefront (edited October 19, 2000).]

Reply With Quote
  #11  
Old October 19th, 2000, 01:54 PM
freebsd
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
>>Can mod_rewrite function differently on different servers/setups?

Seems to be from your situation.

>>This: http://www.landofthesun.org/foo
>>OR: http://www.landofthesun.org/foo/
>>gives: "foo" as the output

So I can confirm that the problem lies between the index.php?category=$1&id=$2 in your situation.
What you can try is to escape the & character like so:

index.php?category=$1&id=$2

Lemme know what you get this time.

Reply With Quote
  #12  
Old October 19th, 2000, 04:02 PM
forefront forefront is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2000
Posts: 165 forefront User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 55 m 52 sec
Reputation Power: 9
>>>Lemme know what you get this time.

I get satisfaction on the PHP-front - on the filesystem front, real directories are still being picked up as "category" variables though files.shtml files.css files.txt etc., are being presented as files even if in a physical directory. Weird! If you can thunk of one final tweak for the dir issue...gr8

That escaping did the trick. Thank you very much for your help. I have something for you to thank you - email me so I can pass it on to you.

Raised some questions re. mod_rewrite though!

Regards
David

[This message has been edited by forefront (edited October 19, 2000).]

Reply With Quote
  #13  
Old October 19th, 2000, 04:40 PM
freebsd
Guest
Dev Shed Newbie (0 - 499 posts)
 
Posts: n/a  
Time spent in forums:
Reputation Power:
>>real directories are still being picked up as "category" variables

Right, I realized that. Let's add a RewriteCond and RewriteRule to the previous rules:
#############################################
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(images|css|data)/?.*$
RewriteRule ^(.+) - [L]
RewriteRule ^([A-Za-z0-9_]+)/?$ index.php?category=$1 [T=application/x-httpd-php,L]
RewriteRule ^([A-Za-z0-9_]+)/([A-Za-z0-9_]+)/?$ index.php?category=$1&id=$2 [T=application/x-httpd-php,L]
#############################################

Once again, when you are really thru with mod_rewrite test, be sure to move everything to httpd.conf and DO NOT enable RewriteLog.

Another tip, if http://www.landofthesun.org is a virtual domain, place the mod_rewrite stuffs from your .htaccess to within your <VirtualHost 207.159.137.10> block.
If you want your other vhost to do the same thing, place the mod_rewrite stuffs outside the <VirtualHost> block and add the following within EACH <VirtualHost 207.159.137.10> block:

RewriteEngine on
RewriteOptions inherit

Another thing, your http://www.landofthesun.org/.htaccess is visible right now, be sure to deny access later on by adding the following to your httpd.conf:

<Files ~ "^.ht">
Order deny,allow
Deny from all
</Files>

>>I have something for you to thank you - email me so I can pass it

This is a public forum, I'd never expect a gift or something in return for helping others. That's okay and thanks for your offer anyway.
Plus, don't feel bad to post further mod_rewrite questions if you have more.

[This message has been edited by freebsd (edited October 19, 2000).]

Reply With Quote
  #14  
Old October 19th, 2000, 05:25 PM
forefront forefront is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2000
Posts: 165 forefront User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 55 m 52 sec
Reputation Power: 9
That rule did the trick. gr8!

>>>Once again, when you are really thru with mod_rewrite test, be sure to move everything to httpd.conf and DO NOT enable RewriteLog.

I tried that between the
<VirtualHost>
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/(images|css|data)/?.*$
RewriteRule ^(.+) - [L]
RewriteRule ^([A-Za-z0-9_]+)/?$ index.php?category=$1 [T=application/x-httpd-php,L]
RewriteRule ^([A-Za-z0-9_]+)/([A-Za-z0-9_]+)/?$ index.php?category=$1&id=$2 [T=application/x-httpd-php,L]
</VirtualHost>

and it didn't work. Nothing, as if it didn't exist. I suspect VServers have some odd setups of Apache.

>>>>>>Another thing, your http://www.landofthesun.org/.htaccess is visible right now, be sure to deny access later on by adding the following to your httpd.conf:

<Files ~ "^.ht">
Order deny, allow
Deny from all
</Files>

Again, same problem - added that on its own outwith any virtualhost directives and can still see the .htaccess (though I now removed the .htaccess anyway!

David

Reply With Quote
  #15  
Old October 19th, 2000, 05:30 PM
forefront forefront is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2000
Posts: 165 forefront User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 55 m 52 sec
Reputation Power: 9
Another topic though relevant to my last comment - do you know of any good hosting companies that provide BSD/Linux hosting Apache/PHP/MySQL, etc., PLUS allow multiple domain hosting (as vservers do..their saving grace!)

Up to about $100 a month.

David

Reply With Quote
Reply

Viewing: Dev Shed ForumsSystem AdministrationApache Development > Final mod_rewrite example


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump