简体   繁体   English

.htaccess动态网址的重写规则

[英].htaccess rewrite rules for Dynamic URL

I would like to create url rewrite rule for my dynamic website using .htaccess file from: 我想使用来自以下位置的.htaccess文件为动态网站创建url重写规则:

http://localhost/pincode/pinpo.php?po=Ameda

to: 至:

http://localhost/pincode/postoffice/Ameda.php

How to change this? 如何改变呢?

Use this in your pincode/.htaccess file 在您的pincode/.htaccess文件中使用它

   RewriteEngine on
  RewriteBase /pincode/
RewriteRule ^postoffice/(.*)\.php$ /pincode/pinpo.php?po=$1 [QSA,NC,L]

From the point of view of server the process is reverse, it rewrites your forged static url to dynamic url that works to retrieve the dynamic content. 从服务器的角度来看,该过程是相反的,它将伪造的静态url重写为可检索动态内容的动态url。

Your url in your website (request_uri) should be 您网站(request_uri)中的网址应为

http://localhost/pincode/postoffice/Ameda.php HTTP://localhost/pincode/postoffice/Ameda.php

And you .htaccess file in the webroot should contain: 而且,您在webroot中的.htaccess文件应包含:

    RewriteEngine on
    RewriteBase /
    RewriteRule ^pincode/postoffice/(.*)\.php$ /pincode/pinpo.php?po=$1 [L]

In case you want to rewrite dynamic to look static, you still need two rewrite rules in order your dynamic data retrieval works, this way: 如果您想将动态重写为静态,则仍然需要两个重写规则才能通过这种方式进行动态数据检索:

    RewriteEngine on
    RewriteBase /                                
    #static to dynamic, with nol arg to prevent infinite looping    
    RewriteRule ^pincode/postoffice/([a-z0-9]+).php$ pincode/pinpo.php?nol&po=$1 [L]
    #dynamic to static:
    RewriteCond %{QUERY_STRING} ^po=([a-zA-Z0-9]+)$
    RewriteRule ^/pincode/pinpo.php$ pincode/postoffice/%1.php [L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM