Rewrite URL in PHP with mod_rewrite

社会主义新天地 提交于 2020-01-22 03:33:05

问题


The web server is Apache. I want to rewrite URL so a user won't know the actual directory. For example: The original URL:

http://www.example.com/en/piecework/piecework.php?piecework_id=11

Expected URL:

http://piecework.example.com/en/11

How to achieve it with mod_rewrite?


回答1:


You need to define a rewrite rule (should be similar to this):

RewriteRule ^/(.*)/en/piecework/(.*)piecework_id=([0-9]+)(.*) piecework.mydomainname.com/en/$3

and put it in a .htaccess file under the main folder of you site

Full description of Rewrite rules here: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html

EDIT

Made a mistake in my rule, hopefully I corrected it.




回答2:


I found a fairly well written crash course in doing so here:
http://articles.sitepoint.com/article/guide-url-rewriting

You'll need to specify the rules in .htaccess (in your site root).




回答3:


I would suggest the following rule:

RewriteCond %{HTTP_HOST} ^(?!www)([^.]+)\.mydomainname\.com$ [NC]
RewriteRule ^(w+)/(\d+)$ /$1/%1/%1.php?%1_id=$2 [L]


来源:https://stackoverflow.com/questions/2460851/rewrite-url-in-php-with-mod-rewrite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!