【问题标题】:Url rewriting for hiding folder name and open it without folder name用于隐藏文件夹名称的 URL 重写并在没有文件夹名称的情况下打开它
【发布时间】:2016-05-05 11:56:03
【问题描述】:

我在根目录中使用了我所有的 uls,现在我无法更改所有这些,所以我需要重写隐藏文件夹名称的主 url,如果有人打开该域,则保存在该文件夹中的数据也必须打开,但是文件夹名称不应显示在 url 中,即我有这个

http://example.com/foldername

.我想把它改成

http://example.com

但它应该自动获取文件夹名称数据。如何实现 通过使用“.htaccess”文件

RewriteEngine On
# The following rule tells Apache that if the requested filename
# exists, simply serve it.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
# The following rewrites all other queries to index.php. The 
# condition ensures that if you are using Apache aliases to do
# mass virtual hosting, the base path will be prepended to 
# allow proper resolution of the index.php file; it will work
# in non-aliased environments as well, providing a safe, one-size 
# fits all solution.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L]

这是我当前使用的 .htaccess 文件

【问题讨论】:

  • 这个 htaccess 在哪里?是在/foldername/里面吗?
  • 这可能会对您有所帮助。看看 - stackoverflow.com/questions/17642122/…
  • 是的。你是对的,所以我可以创建一个新的 htaccess 文件来解决我的问题,并且可以保留这个文件,因为它是 @anubhava。
  • 这个 htaccess 可以在 foldername 中使用,但你需要在 root 中使用另一个 htaccess
  • 是的@anubhava,所以你能告诉我这是如何实现的吗?另一个 htaccess 没有问题

标签: php .htaccess mod-rewrite url-rewriting


【解决方案1】:

将此规则保留在根 .htaccess 中:

RewriteEngine On

RewriteRule ^(?!foldername/)(.*)$ foldername/$1 [L,NC]

然后在/fodlername/.htaccess 中有这个简化的代码:

DirectoryIndex index.php
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

【讨论】:

  • 我已经为根文件添加了您的代码,但是对于文件夹文件,我必须用我的代码替换您的代码还是将其附加到我的代码中。你能把它添加到我的 htaccess 文件中吗?会有很大帮助的。
  • 你可以用我建议的/fodlername/.htaccess替换所有的.htaccess问题代码
  • 此“在此服务器上未找到请求的 URL /index.php。此外,在尝试使用 ErrorDocument 处理请求时遇到 404 Not Found 错误。Apache/2.4.10 ( Unix) OpenSSL/1.0.1e-fips mod_bwlimited/1.4 PHP/5.5.19 服务器在 rustagisamaj.com 端口 80"
  • 我需要离线工作,一小时后参加
  • 我认为您需要编辑已经给出一些条件的文件。
【解决方案2】:

像这样更改您的 .htaccess 文件

RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /foldername/
RewriteCond %{HTTP_HOST} ^(www\.)?yoursite.com$
RewriteRule ^foldername/(.*) /$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?yoursite.com$
RewriteRule !^foldername/ foldername%{REQUEST_URI} [L]

【讨论】:

  • 虽然这段代码可以回答这个问题,但最好包含一些上下文,解释它是如何工作的以及何时使用它。从长远来看,纯代码的答案没有用处。
  • 你说得对。但是如果问题不清楚并且问题没有重复,例如这里有很多人询问这个问题,但最多将特定目录中的所有内容重定向到父目录或根目录,但这里的区别是他想阻止直接浏览原始路径并且当他们探索它时,它会被隐藏,所以从上面的讨论中我认为提问者知道他需要什么,所以我不加评论就写出来,谢谢你的评论
  • 答案也应该能够帮助其他有同样问题的人,而不仅仅是最初的提问者,记住这一点;)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-14
  • 1970-01-01
  • 2015-08-12
  • 2012-03-30
  • 2013-07-18
相关资源
最近更新 更多