【问题标题】:Hide directory name from URL by .htaccess通过 .htaccess 从 URL 中隐藏目录名称
【发布时间】:2014-07-08 15:31:48
【问题描述】:

当用户键入时:

www.domain.com/standard/Public/Modules/home.php or 
www.domain.com/standard/Public/Modules/contacts.php

(或任何遵循相同模式的东西)

我希望 URL 显示如下:

www.domain.com/home
www.domain.com/contacts
...

我当前的 .htaccess:

RewriteEngine on 
RewriteRule    ^home.php/?$   standard/Public/Modules/home.php  [NC,L] 
RewriteRule    ^contacts.php/?$   standard/Public/Modules/contacts.php  [NC,L] 

【问题讨论】:

  • 通常这不是通过重定向来实现的,而是通过使用所谓的Front Controller Pattern,您可以从一开始就定义您的路线以调用特定的代码片段。我喜欢为此使用Slim Framework

标签: php regex apache .htaccess mod-rewrite


【解决方案1】:

例如

<IfModule mod_rewrite.c>
    RewriteEngine On                         

    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)-product(.*).html$ /products/view/$2 [L]
    RewriteRule ^(.*)-cat(.*).html$ /products/listing/$2 [L]
    RewriteRule ^products.html$ /products/listing/ [L]
    RewriteRule ^bioproducts.html$ /products/bio_listing/ [L]
    RewriteRule ^aboutus.html$ /static/index/15 [L]
    RewriteRule ^contacts.html$ /static/contacts [L]
    RewriteRule ^(.*)-partners(.*).html /partners/index/$2 [L]



</IfModule>

【讨论】:

    【解决方案2】:

    你需要在你的根 .htaccess 中有这个:

    RewriteEngine on
    
    RewriteCond %{THE_REQUEST} /standard/Public/Modules/login\.php [NC]
    RewriteRule ^standard/Public/Modules/login\.php$ /home [L,NC,R=301]
    
    RewriteCond %{THE_REQUEST} /standard/Public/Modules/logout\.php [NC]
    RewriteRule ^standard/Public/Modules/logout\.php$ /contacts [L,NC,R=301]
    
    RewriteRule ^home/?$ /standard/Public/Modules/login.php  [NC,L] 
    RewriteRule ^contacts/?$ /standard/Public/Modules/logout.php  [NC,L] 
    

    【讨论】:

    • 嗨@anubhava,它仍然不起作用,我应该将 .htaccess 文件放在 Modules 文件夹中吗?我在最后一个代码部分也有一个错误,现在它是正确的。
    • 你已经有/standard/Public/Modules/.htaccess文件了吗?
    • 我的 .htaccess 文件目前在 /standard/ 文件夹中,我想我应该把它移到 Modules 文件夹中,对吧?
    • 不行,需要在standard的父目录下
    • 对于 css/js 只需确保使用绝对 URL,即以 /http:// 开头
    【解决方案3】:

    我用这个,不需要单独设置页面重写

    RewriteEngine on 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME}\.html -f 
    RewriteRule ^(.*)$ $1.html
    

    【讨论】:

    • 没关系。重新阅读问题并意识到这不是您想要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-06-26
    • 2016-09-22
    • 1970-01-01
    • 2020-01-31
    • 2017-05-06
    • 2014-04-04
    • 2011-03-28
    相关资源
    最近更新 更多