1
WarDick
Making Dynamic URLs Static
  • 2004/1/22 14:15

  • WarDick

  • Just can't stay away

  • Posts: 890

  • Since: 2003/9/13


I ran across this bit of information this morning while drinking coffee. I thought I would share it with the community and ask for your advice. Would this be practical within the structure of XOOPS and PHP or No.

Quote:
Making Dynamic URLs Static
or how to have a database driven site with no query strings appearing in the urls

The method used on this site relies on an asp custom 404 error page (it needs to be set as a URL, not a file). I first heard of this technique at 4 Guys From Rolla in an article describing how it was used to manage redirects of about 6000 pages when the msdn.microsoft.com site was restructured. I have also drawn on other sources so I claim no credit for any originality on my part!

The error handler first analyses the url request and only then executes the necessary database query. The important snippet of asp code looks something like this:-

' step one grab the identity of the page from the query sent to the 404 page:-
strOrigURL = lcase(request.servervariables("QUERY_STRING") )

' step two check if it meets your criteria or is a genuine 404, in my case, I just wanted to check that the page requested belonged to a particular directory and had a numeric name.

if instr(strOrigURL,"/cinereviews/") >0 then

' step three - collect the numeric id requested

intStart = instr(strOrigURL,"/cinereviews/") + 13
intReview = replace(mid(strOrigURL,intStart),"/","")

' step 4 go fetch the record from the database in the usual way

mySQL = "select * from Reviews where id=" & intReview %>
This technique can be used to build whole sites, power a CMS or just serve pages within one directory.

Do test thoroughly that the page when served returns a 200 ok code to the browser since search engines will choke on 304 or 404 codes. It should do, but it is worth checking.

Your page usage stats will probably be confused by this technique so it is worth setting up an additional script to analyse details of the requests for your custom 404 page.

You can adapt this technique so that it uses a proper name for pages instead of the numeric ID which I use. In my case I was concerned to ensure that the page names were always unique and the obvious field in my database (the Movie Title) did not, on its own, ensure that this would always be the case. There are benefits in having a meaningful url since some search engines use the url in their ranking algorithm.

The detail of what you will need to add to your code will vary according to your application, but I hope I have described the principle adequately. Good luck and let me know if you need more information.

Tim Marchant

2
Draven
Re: Making Dynamic URLs Static
  • 2004/1/22 15:55

  • Draven

  • Module Developer

  • Posts: 337

  • Since: 2003/5/28


Same technique for PHP can be found athttp://www.sitepoint.com/article/485/2, but I still think the mod_rewrite is the way to go. This way is slower and can mess with log files,stats and so on since everything is logged as a 404 error page.

3
DonXoop
Re: Making Dynamic URLs Static

Wow, the first one depends on ASP and a 404 error page? The next relies on PHP?

mod_rewrite is the only proper way IMHO. Oh wait, that is an Apache thing. Must be the same thing available for IIS?

mod_rewrite is a black art but oh so powerful. Apache mod_rewrite manual

4
WarDick
Re: Making Dynamic URLs Static
  • 2004/1/22 16:18

  • WarDick

  • Just can't stay away

  • Posts: 890

  • Since: 2003/9/13


I too believe that mod_rewrite could be the answer but I have never been abel to find a clear answer to my question about it. Maybe someone here could help out. Simply I want to know if mod_rewrite has the ability to write whatever I want into the address window of the clients browser. It seems a direct question but one I can not get clear in my mind.

Thanks for any insight.

5
sunsnapper
Re: Making Dynamic URLs Static

To answer your question, I believe if you end your statement with a [R] (for redirect) instead of [L] it will update the user's URL bar to show the new URL.

If you only have [L] the page passes to the user transparently, without them seeing the "true" URL. Note that [L] serves to indicate the "Last Line" that should be processed if the rule is a match for the current request.

[R] actually forcefully moves them to an actual new URL that they can see.

If I am correctly guessing at what you are trying to do... you want users with outdated links to be moved to the new link format. For that you can use [R]

However, you would probably then want a series of [L] commands to intercept your new readable URLs and pass them transparently to the XOOPS URL format.

There is a thread on this here:
Apache mod_rewrite RewriteRule Success

6
Draven
Re: Making Dynamic URLs Static
  • 2004/1/22 17:02

  • Draven

  • Module Developer

  • Posts: 337

  • Since: 2003/5/28


Actually, I think he means the URL's printed to the screen within the document. The answer is no, mod_rewrite doesn't do that. You need a function to rewrite all your URL's to the SE friendly ones. Mod_rewrite only takes care of the URL once the request for it is made, it converts it back to the "real" url. But in your documents you need a function to changehttp://mysite.com/modules/wfsections/index.php?articleid=1 tohttp://mysite/articles/1/, mod_rewtire doesn't do that, only the backwards, coverting it from the second back to the first transparently to load the document.

7
WarDick
Re: Making Dynamic URLs Static
  • 2004/1/22 17:12

  • WarDick

  • Just can't stay away

  • Posts: 890

  • Since: 2003/9/13


Thanks for your insight and patients. It's all starting to make sense now.

8
Draven
Re: Making Dynamic URLs Static
  • 2004/1/22 17:27

  • Draven

  • Module Developer

  • Posts: 337

  • Since: 2003/5/28


No problem. Using mod_rewrite is the easy part, the big problem is in righting the URL's to the screen. Because all modules do things differently, there's no way to ensure ALL links that are in a document get written the correct way. YOu can get some, like ones submitted through text fields, by altering the text sanatizer functions (Providing that module makes use of the text sanatizer, which some don't), but the other hard coded urls all must be wrapped in a function to write them in the SE friendly manner. This requires updating ALL the modules to wrap every url in a mod_rewrite("index.php?var=val","module name", suedo name"); type function to display links the proper way.

9
mvandam
Re: Making Dynamic URLs Static
  • 2004/1/22 17:28

  • mvandam

  • Quite a regular

  • Posts: 253

  • Since: 2003/2/7 2


I have heard that a quite portable (i.e. works on many web servers) way to rewrite the URLs is something like:

http://example.com/index.php/articles/1/

(Note the presense of 'index.php'.)

I first saw this done in 'phpwiki'. Here is a little excerpt from the comments of the file index.php (in phpwiki):

Quote:

* PhpWiki will try to use short urls to pages, eg
*http://www.example.com/index.php/HomePage
* If you want to use urls like
*http://www.example.com/index.php?pagename=HomePage
* then define 'USE_PATH_INFO' as false by uncommenting the line below.
* NB: If you are using Apache >= 2.0.30, then you may need to to use
* the directive "AcceptPathInfo On" in your Apache configuration file
* (or in an appropriate <.htaccess> file) for the short urls to work:
* Seehttp://httpd.apache.org/docs-2.0/mod/core.html#acceptpathinfo

10
Draven
Re: Making Dynamic URLs Static
  • 2004/1/22 17:31

  • Draven

  • Module Developer

  • Posts: 337

  • Since: 2003/5/28


Quote:

mvandam wrote:
I have heard that a quite portable (i.e. works on many web servers) way to rewrite the URLs is something like:

http://example.com/index.php/articles/1/

(Note the presense of 'index.php'.)

I first saw this done in 'phpwiki'. Here is a little excerpt from the comments of the file index.php (in phpwiki):

Quote:

* PhpWiki will try to use short urls to pages, eg
*http://www.example.com/index.php/HomePage
* If you want to use urls like
*http://www.example.com/index.php?pagename=HomePage
* then define 'USE_PATH_INFO' as false by uncommenting the line below.
* NB: If you are using Apache >= 2.0.30, then you may need to to use
* the directive "AcceptPathInfo On" in your Apache configuration file
* (or in an appropriate <.htaccess> file) for the short urls to work:
* Seehttp://httpd.apache.org/docs-2.0/mod/core.html#acceptpathinfo


This still misses the fact that what we are trying to do is eliminate the things that trip up most web crawlers, one of those being the presence of "periods" in the URL (the /index.php/ part in a directory list).

Login

Who's Online

200 user(s) are online (117 user(s) are browsing Support Forums)


Members: 0


Guests: 200


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Apr 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits