问题
Is there a way to insert relative URLS in php code such as /forums/(forumID)/
into tags while setting up my site? Then when I am trying to get which forumID the current page is, to get it via a $_GET
request without using a template system like Smarty, CakePHP etc or Apache rewrite module? Or is it a huge headache? I just want to be able to not be bound to one web server type (Apache).
回答1:
... carried on from OP comments.
These frameworks read the request again in their respective languages to allow the framework to route to specific controllers, but they still need the webserver to be setup to send the request to the framework in the first place.
- Client requests http://example.com/forums/123
- After DNS lookup, request hits server at 127.0.0.1:80
- Webserver (eg Apache) listens for port 80 and accepts request
- Apache routes request to server-side script
This is the journey as the web server sees it. It needs to read the request before it even hits the server-side scripting language (PHP/Python/Ruby etc).
The server-side languages can then re-read the URL once the webserver has hit the front controller as they please.
回答2:
Clean urls are fairly easy to do, but if the web pages are vastly different, it may cause some problems.
You'll need to edit your .htaccess file and add something similar to this
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)$ index.php?page=$1
#This will process http://example.com/forum as http://example.com/index.php?page=forum
RewriteRule ^([a-zA-Z0-9]+)/$ index.php?page=$1
#This includes the trailing slash
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ index.php?page=$1&id=$2
#This will process http://example.com/forum/512 as http://example.com/index.php?page=forum&id=512
This is a good source for more information http://www.desiquintans.com/cleanurls
回答3:
The main reasons to have clean urls from an architecture point of view:
- They are not tied to any programming language. If you have
.php
or any other extensions, you'd have to set up your server to accept.php
extensions for other languages if you switch to ASP.net. - They are are easy to route in any language or server setup. All modern servers I know of have modules to route urls.
Note that to use a programming language to route the urls, you still have to set up your server to direct everything to a bootstrap file. Honestly, you are not getting around server configurations of some kind no matter what.
Conclusion: your logic for wanting your project set up this way will not work without doing some server setup.
来源:https://stackoverflow.com/questions/8394424/make-clean-urls-and-retrieve-query-string