【问题标题】:htaccess need help rewriting urlhtaccess 需要帮助重写 url
【发布时间】:2013-03-15 22:48:15
【问题描述】:

我一直在尝试用 htaccess 文件重写我的 URL。 我的项目使用我的 index.php 作为它的基本控制器。 我使用获取值“naam”从数据库中获取页面。

示例: localhost/YourDesigns/YDCMS/index.php?naam=home(显示主页) localhost/YourDesigns/YDCMS/index.php?naam=about(这显示了关于页面)

现在我想重写要显示的 URLS: localhost/YourDesigns/YDCMS/home(显示主页) localhost/YourDesigns/YDCMS/about(这显示了关于页面)

我自己做了一些 htaccess 的东西,我已经成功地从 url 中删除了“index.php”,但“naam”仍然存在。

如何删除它? 这是我当前的 htaccess 文件:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /YourDesigns/YDCMS/index.php?naam=/$1 [L]

提前致谢:)

【问题讨论】:

    标签: php .htaccess url url-rewriting


    【解决方案1】:

    我认为您查看该过程的方式并不完全正确,如果您想像 localhost/YourDesigns/YDCMS/home 那样显示 url,您必须首先将 html 内容中的 url 替换为该格式,然后使用 RewriteRule 您内部调用正确的路径:

    RewriteRule ^desired_url_path/([a-z0-9\-]+)$ /base_url_path/index.php?naam=$1 [L]
    

    这样,当用户单击链接时,Apache 服务器将使用上述正则表达式通过一个规则转换 url,该规则基本上说:基本 url 之后的所有内容都是要在 index.php 上传递的参数值。 当然,可以修改正则表达式以满足您的需要,我在上面写的那个是基本的字符串模式。

    【讨论】:

    • 感谢您的回答!所以如果我的项目目录是 localhost/yourdesigns/YDCMS/ 我的 htaccess 会是这样吗? RewriteRule ^YourDesigns/YDCMS/([a-z0-9\-]+)$ /YDCMS/index.php?naam=$1 [L] (对不起,我仍然需要弄清楚如何在stackoverflow xD中换行)
    • 我已经对您的代码进行了一些编辑(不确定它是否正确),但它现在可以按预期工作!结果如下: RewriteRule ^(.*)$ /YourDesigns/YDCMS/index.php?naam=$1 [L]
    • 是的,如果您使用的是 xampp/lampp 安装,我认为这是正确的。请记住,该规则必须与您的网站基本网址相关,并且您已设置好:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多