【问题标题】:modrewrite website with slash in .htaccess.htaccess 中带有斜线的 mod_rewrite 网站
【发布时间】:2012-02-16 03:36:52
【问题描述】:

如何翻译如下网址: http://localhost/mySite/?link=OFFERS&sublink=ARTICLE&subsublink=DETAIL

到: http://localhost/mySite/OFFERS/ARTICLE/DETAIL

如果一个参数是空的,它应该仍然像这样工作: localhost/mySite/?link=OFFERS&sublink=ARTICLE 本地主机/mySite/OFFERS/ARTICLE

额外问题:index.php 下有一个进入页面,重写应该与 index2.php 一起工作。最好是它可以在 localhost 和实时系统上运行而无需更改。

目前我正在使用:RewriteRule ^([^/.]+)$ index2.php?link=$1 [L] 但这仅适用于一个参数,我无法改进这段代码多年^^

【问题讨论】:

  • 进入页面是什么意思?你的意思是进入站点/离开站点?
  • 只是重定向不适用于 index.php -> 不仅适用于 /?link=asdf。但是对于 /index2.php?link=asdf index.php 只是一个带有一些内部链接的欢迎页面
  • 我还是不明白。您的意思是要将index.php/?link=asdf/?link=asdf 重定向到/index2.php?link=asdf 吗?
  • 使用 modrewrite 可以重定向到一个站点并以可识别的方式提供该站点参数。这里的目标站点应该是index2.php。通常它是从 localhost/mysite/link 到 localhost/mysite/?link=asdf 的重定向。但我希望它到 localhost/mysite/index2.php?link=asdf。希望对您有所帮助...

标签: .htaccess mod-rewrite slash


【解决方案1】:
RewriteRule ^([^/.]+)(/([^/.]+))?(/([^/.]+))?$ index2.php?link=$1&sublink=$3&subsublink=$5 [L,QSA]

注意localhost/mySite/OFFERS/ARTICLE 链接到localhost/mySite/?link=OFFERS&sublink=ARTICLE&sussublink= 而不是localhost/mySite/?link=OFFERS&sublink=ARTICLE。 应该不是什么大问题,但请确保 PHP 代码不是我们isset($_GET['subsublink'])

【讨论】:

    【解决方案2】:

    尝试将以下内容添加到您网站的mysite目录中的 htaccess 文件中。

    RewriteEngine on
    RewriteBase /mysite/
    
    # rewrite http://localhost/mySite/OFFERS/ARTICLE/DETAIL to
    # http://localhost/mySite/?link=OFFERS&sublink=ARTICLE&subsublink=DETAIL
    #assumes that index2.php is in the root directory of site
    RewriteRule ^([-a-zA-Z0-9])+/([-a-zA-Z0-9])+/([-a-zA-Z0-9])+ index2.php?link=$1&sublink=$2&subsublink=$3 [NC,L]
    
    #redirect index to index2
    #if you do not want to redirect, just server rewrite, remove the R=301 below
    RewriteRule ^index\.php$ index2.php [NC,L,R=301]
    

    【讨论】:

    • 我试图让这段代码工作,但只得到一个 404 错误... index、index2 和 .htaccess 文件位于 xampp/htdocs/mysite/
    • @yourmother ok 编辑以反映所有内容都在 mysite 目录中。
    • localhost/mysite/OFFERS 上的 404 错误。如果我修改代码以仅适合 1 个参数而不是三个参数,则显示站点但看起来很糟糕(图像路径和文件路径似乎无法正常工作)
    猜你喜欢
    • 2016-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-07
    • 2016-06-06
    • 2014-09-16
    • 1970-01-01
    相关资源
    最近更新 更多