【问题标题】:URL Rewriting for php files in web.configweb.config 中 php 文件的 URL 重写
【发布时间】:2017-01-29 17:33:05
【问题描述】:

我在这里遇到了一些问题,我在这里检查了另一篇帖子的所有答案,但我遇到了 500 错误,或者什么也没发生。

问题是:

假设我有一个类似的网站:flan.com

所以,我有一个动态 URL,例如:

flan.com/index.php?page=25flan.com/news.php?news=aqweqwr546

请注意,此值“aqweqwr546”是动态的,可以是数字或字母或两者兼有。

我需要这样的东西:

第一个:flan.com/page/a title shown here

第二个:flan.com/news/a title shown here

或类似用户友好的 URL!

请帮助我。在此先感谢

【问题讨论】:

    标签: php url-rewriting web-config


    【解决方案1】:

    像这样重写你的网址......

    RewriteEngine On
    #For home page. i.e. index.php
    RewriteRule ^page/([a-zA-Z0-9]+)$ index.php?page=$1 [L]
    
    #For news page i.e news.php
    RewriteRule ^news/([a-zA-Z0-9]+)$ news.php?news=$1 [L]
    

    web.config 像这样..

    <rewrite>
      <rules>
        <rule name="Rewrite to index.php">
          <match url="^page/([a-zA-Z0-9]+)$"/>
          <action type="Rewrite" url="index.php?page={R:1}/>
        </rule>
        <rule name="Rewrite to news.php">
          <match url="^news/([a-zA-Z0-9]+)$"/>
          <action type="Rewrite" url="news.php?news={R:1}/>
        </rule>
      </rules>
    </rewrite>
    

    【讨论】:

    【解决方案2】:

    在您网站的根目录中创建或编辑.htaccess 文件,然后输入:

    RewriteEngine On
    
    RewriteRule /(.*)/$ news.php?news=$1
    

    这将为您提供如下所示的网址:

    flan.com/aqweqwr546/
    

    或者这个:

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

    会给你这个:

    flan.com/news/aqweqwr546/
    

    【讨论】:

    • 对不起,我有 web.config 而不是 .htaccess,因为服务器在 windows 中运行,怎么可能在 web.config 中?
    • 我不熟悉 web.config。但是,根据this tool,是这样的:&lt;rule name="rule 1N"&gt;&lt;match url="/(.*)/$" /&gt;&lt;action type="Rewrite" url="/news.php?news={R:1}" /&gt;&lt;/rule&gt;
    • 添加后,网站没有任何反应
    猜你喜欢
    • 2015-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多