|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
|
|
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here! |
|
#1
|
|||
|
|||
|
URL Using scripting technologies such PHP, ASP, CGIs, Servlets one can call a URL thus: www.foo.com/bar.html?id=3 I need a *specific* example of how I can use mod_rewrite (in srm.conf or .htaccess) to change this so people can write /content/page3.html and the content from "/content/page.html?id=3" gets served up? So, supposing all files within the folder /content/ on our webserver are called via /content/page.shtml?id=3 /content/page.shtml?id=4 etc., what would be the rewrite example to make this go: /content/page3.shtml /content/page4.shtml , etc., Many thanks in advance. David |
|
#2
|
|||
|
|||
|
Actually, this can be accomplished without mod_rewrite, if you want:
In httpd.conf (or with .htaccess) change the ErrorDocument 404 directive to point to a .php page (such as /content/page.php) which will parse the $REQUEST_URI variable. Thus if the browser requests: http://www.your_server.com/content/page4.html This file doesn't exist, so output gets redirected through http://www.your_server.com/content/page.php This PHP page can simply read $REQUEST_URI, which in this case is "/content/page4.html" and manipulate it to arrive at the recordset id. (an example using MySQL with PHP) <?php $pageid = substr($REQUEST_URI, 13, -5); $db = mysql_connect("localhost", "user", "password"); $query = "SELECT * FROM pages WHERE id = $pageid"; $result = mysql_db_query("databasename", $query); etc... ?> The important thing to note here is the function substr(string, [start], [length]) with returns a substring of a given string starting at [start] and ending at [length], so we can take the value "/content/page4.html" start at the 13th character, and remove the last 5 characters and come back with "4", which we read as the recordset id number. Since we use -5 as the second parameter it will remove the ".html" from the end; even if we are calling "/content/page238.html", we will end up with $pageid = 238 All the while, the original URL request still shows in the browser location bar. You like? :-) [This message has been edited by rycamor (edited July 06, 2000).] |
|
#3
|
|||
|
|||
|
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by rycamor:
Actually, this can be accomplished without mod_rewrite, if you want: In httpd.conf (or with .htaccess) change the ErrorDocument 404 directive to point to a .php page (such as /content/page.php) which will parse the $REQUEST_URI variable. You like? [/quote] I like :-) Users tend to foobar so much so why not create a system that says "Please foobar as much as you like!" And using .htaccess it can be done directory-wide rather than site wide. Yeah... URL Still would like to learn the intricacies of mod_rewrite though ;-) Thanks for the tip. The only thing missing is a catchall "real 404" page for anyone that types a page whose id doesn't exist :-) Andy script modification appreciated for this! [This message has been edited by forefront108 (edited July 07, 2000).] [This message has been edited by forefront108 (edited July 07, 2000).] |
|
#4
|
|||
|
|||
|
Thats very neat... Just a few questions:
Will that degrade server performance and/or page generation time since it has to go via a 404? Whats mod_rewrite like on the server's performance? And wouldn't that muck up your server stats since it would be counting tons of 404's? This would only be a concern if you had something like webalizer and wanted some useful data from it... |
|
#5
|
|||
|
|||
|
I would like to use mod_rewrite to rewrite dynamic/static urls. What would be the regex used in the virtual server tag to make
http://www.domain.com/12.html the same as http://www.domain.com/?page_id=12 ?? If someone could let me know this I would really appreciate it. |
|
#6
|
|||
|
|||
|
This could be a dumb question, but is mod_rewrite the kind of stuff that all those big sites with urls like 1000,5687,2468.html and stuff like that use?
|
|
#7
|
|||
|
|||
|
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by #6:
This could be a dumb question, but is mod_rewrite the kind of stuff that all those big sites with urls like 1000,5687,2468.html and stuff like that use?[/quote] It could be the method they use or it could be another method. The suggestion in this forum to simply make a PHHP script server up all 404 errors is not an eloquent solution and we are left with a difficult mod_rewrite manual with hazy examples. mod_rewrite is definitely a pig though the lack of good examples for dynamic sites makes it all the more esoteric. |
|
#8
|
|||
|
|||
|
>>http://www.domain.com/12.html the same as http://www.domain.com/?page_id=12 ??
It's entirely up to your index.cgi or index.php or whatever name of your script. Your script itself MUST be able to know what page_id=12 is and display the exact content of 12.html. Your situation can use mod_rewrite ONLY to redirect page request from http://www.domain.com/12.html to http://www.domain.com/?page_id=12 All the posters on this page have the same situation. ############################################# RewriteEngine on RewriteRule ^([A-Za-z0-9]+).html?$ http://www.domain.com/?id=$1 [R] ############################################# In this example, assuming all request html document is in the document root directory and a request of http://www.domain.com/abc123.html will be redirected to http://www.domain.com/?id=abc123 Nobody can ever request your abc123.html page directly via browser. Once again, your script itself MUST be able to read abc123.html and display it to screen. In addition, if a request html document is not found, your script needs to be able to display a 404 not found error. Does this make it clear? |
|
#9
|
|||
|
|||
|
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by freebsd:
>>http://www.domain.com/12.html the same as http://www.domain.com/?page_id=12 ?? It's entirely up to your index.cgi or index.php or whatever name of your script. Your script itself MUST be able to know what page_id=12 is and display the exact content of 12.html. Your situation can use mod_rewrite ONLY to redirect page request from http://www.domain.com/12.html to http://www.domain.com/?page_id=12 All the posters on this page have the same situation. ############################################# RewriteEngine on RewriteRule ^([A-Za-z0-9]+).html?$ http://www.domain.com/?id=$1 [R] ############################################# In this example, assuming all request html document is in the document root directory and a request of http://www.domain.com/abc123.html will be redirected to http://www.domain.com/?id=abc123 Nobody can ever request your abc123.html page directly via browser. Once again, your script itself MUST be able to read abc123.html and display it to screen. In addition, if a request html document is not found, your script needs to be able to display a 404 not found error. Does this make it clear?[/quote] Not entirely clear, no. You imply that a person cannot type in abc123.html but that is the whole point - that they type in abc123.html, the URL gets rewritten and your PHP or whatever script is called to serve up the content of page.html?id=123 Rewriting the URL to *include* a "?" defeats the purpose of rewriting the URL in the first place. Perhaps this is not what you meant but it is what you conveyed. |
|
#10
|
|||
|
|||
|
>>Rewriting the URL to *include* a "?" defeats the purpose of rewriting the URL in the first place.
Exactly. Because their cases really have nothing to do with mod_rewrite, as I said, it's up to their scripts to do the task. Their situation should only use mod_rewrite to REDIRECT, not REWRITE request. The only dynamic part is to take the "abc123" part of the "abc123.html" and put it in the query string of the script. All requests should be made directly to http://www.domain.com/?id=abc123. The only purpose is to "deny direct access to the html documents". I assume they don't want to put a load to apache for every document requests. [This message has been edited by freebsd (edited September 19, 2000).] |
|
#11
|
|||
|
|||
|
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by freebsd:
>>Rewriting the URL to *include* a "?" defeats the purpose of rewriting the URL in the first place. Exactly. Because their cases really have nothing to do with mod_rewrite, as I said, it's up to their scripts to do the task. Their situation should only use mod_rewrite to REDIRECT, not REWRITE request. The only dynamic part is to take the "abc123" part of the "abc123.html" and put it in the query string of the script. All requests should be made directly to http://www.domain.com/?id=abc123. The only purpose is to "deny direct access to the html documents". I assume they don't want to put a load to apache for every document requests. [This message has been edited by freebsd (edited September 19, 2000).][/quote] The method you are purporting is more "mod_redirect" than mod_rewrite The whole point of this thread was to establish ways of making "oink.html?id=foo" -type URLs search-engine friendly. Maintaining a "?" in the URL will not do that No, the request should come in to iurl.com/page123.html and the PHP script handles it. "?" Should never appear in the URL. We are rewriting NOT redirecting which also foobars search engines. Do you have a specific example of this or can we close discussion of URL redirecting? [This message has been edited by forefront (edited September 19, 2000).] |
|
#12
|
|||
|
|||
|
OK guys maybe I can try to clear this up a little.
What I would like to do is static to dynamic translation so that dynamic pages can be indexed by search engines and recorded in log files. (Note: somenumber and anothernumber are variable numeric placeholders.) So, for example, I would like to make the url http://www.domain.com/somenumber/ the same as http://www.domain.com/?page_id=somenumber. The url with the "?" would never be displayed, but would be called when someone went to http://www.domain.com/somenumber/. I would like this to pickup only those matches that are /somenumber/ and nothing else. Can you help me out with this? Also, I would like to use one more regular expression to do the follow translation: http://www.domain.com/somenumber/anothernumber/ the same as http://www.domain.com/?page_id=some...=anothernumber. If you could help me out with these two regex's I would appreciate it tremendously! |
|
#13
|
|||
|
|||
|
>>So, for example, I would like to make the url http://www.domain.com/somenumber/
the same as http://www.domain.com/?page_id=somenumber Please don't use "the same as" as they are never the same and is misleading to me. What is the exact hyperlink for users to click on the page or to type into the location bar? What URL will be displayed in the location bar? What content from which file (please give full directory path) should be displayed? Show me the code of your index.php. |
|
#14
|
|||
|
|||
|
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by freebsd:
>>So, for example, I would like to make the url http://www.domain.com/somenumber/ the same as http://www.domain.com/?page_id=somenumber Please don't use "the same as" as they are never the same and is misleading to me. What is the exact hyperlink for users to click on the page or to type into the location bar? What URL will be displayed in the location bar? What content from which file (please give full directory path) should be displayed? Show me the code of your index.php.[/quote] The URL someone would type in before rewriting is installed would be: www.mysite.com/index.php?id=3 OR www.mysite.com/?id=3 After rewriting the URL one could type would be: www.mysite.com/index.php/3/ OR www.mysite.com/3/ The URL displayed in the URl bar will be what a person types, nothing will change. That is the whole point of mod_rewrite - that the rewriting is done on the back-end You ask "what content from which file" which is an odd question - if we are calling an "id" it will be from a database, not file on the filesystem (or why use dynamic pages in any case?!) The PHP script could in fact be any PHP script that calls a specific id from a database. The PHP script doesn't change - mod_rwrite "rewrites" (hence the name) the URL so that: 1. The user is happy with a "friendly" URL 2. PHP "thinks" the user passed it a URL with "id=?" in it 3. The same content gets served I do not know the answer, but I do understand the question and the principle under discussion David |
|
#15
|
|||
|
|||
|
>>After rewriting the URL one could type would be:
>>www.mysite.com/index.php/3/ >>OR www.mysite.com/3/ Now it's more understandable to me. So /3/ is not a dir nor a file, instead, it's a query string of your script. Okay, here we go... Example for www.mysite.com/index.php/3/ ############################################ RewriteEngine on RewriteRule ^index.php/(.+)/$ index.php?$1 [T=application/x-httpd-php] ############################################ Example for www.mysite.com/3/ ############################################ RewriteEngine on RewriteRule ^(.+)/$ index.php?$1 [T=application/x-httpd-php] ############################################ Finally, let's say your index.php accepts query string with id=blah_blah, but you don't want the URL to be "www.mysite.com/id=blah_blah/ , here it is for www.mysite.com/3/ ############################################ RewriteEngine on RewriteRule ^(.+)/$ index.php?id=$1 [T=application/x-httpd-php] ############################################ >>mod_rwrite "rewrites" (hence the name) mod_rewrite, the rewrite doesn't imply to rewriting the URL only. mod_rewrite is highly flexible and covers many other tasks - redirecting is one of those. >>That is the whole point of mod_rewrite - that the rewriting is done on the back-end Not neccessary the back-end (internally), it can also be done _externally_ depending on your situation. My 3 examples above are the internal ones. >>change the ErrorDocument 404 directive to point to a .php page Forget about this from now on. >>RewriteEngine on >>RewriteRule ^([A-Za-z0-9]+).html?$ http://www.domain.com/?id=$1 [R] My earlier thought was that you and other posters want a PHP-driven site and hide all *.html pages. [This message has been edited by freebsd (edited September 21, 2000).] |
![]() |
| Viewing: Dev Shed Forums > System Administration > Apache Development > mod_rewrite help |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|