how to simplify the url

2019-03-06 15:05发布

问题:

Hi: In my website I found that their url are very simple like:

http://example.com/questions/4486620/randomaccessfile-probelm

And generally the url should like:

http://example.com/questions?xx=4486620&title=randomaccessfile-probelm

That's to say there is no request parameters combined by "&" in the url.

How did they make it? Is there any framework?

UPDATE: My application works under tomcat.

回答1:

On an apache web server you can do this using mod_rewrite http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html

Here is an example along with a tutorial...

The Apache rewrite engine is mainly used to turn dynamic url’s such as www.yoursite.com/product.php?id=123 into static and user friendly url’s such as www.yoursite.com/product/123

Read more about htaccess And mod_rewrite Tutorial by Blogstorm SEO Blog

RewriteEngine on
RewriteRule ^product/([^/\.]+)/?$ product.php?id=$1 [L]
Another example, rewrite from:
www.yoursite.com/script.php?product=123 to www.yoursite.com/cat/product/123/

RewriteRule cat/(.*)/(.*)/$ /script.php?$1=$2

Read more here that example by the way is copied from http://www.blogstorm.co.uk/htaccess-mod_rewrite-ultimate-guide/ - assigned kudos!