【问题标题】:.htaccess, redirect to subfolder when no url segment.htaccess,当没有 url 段时重定向到子文件夹
【发布时间】:2015-04-09 02:42:09
【问题描述】:

我开发了一个 CI 网站,并托管在服务器根目录上,我还创建了一些静态 html 页面并将它们放在website 目录中。 (见下文)

public_html
     /application
     /other ci folder
     /index.php
     /.htaccess
     /website // in this directory i have my static html website

所以当我输入www.myweb.com 时,我可以访问我的 CI 应用程序, CI 应用有 2 条主要途径。

www.myweb.com for front end
www.myweb.com/admin backend

现在我可以使用 www.myweb.com/website

我想要的是:

当我进入时

www.myweb.com 它应该显示静态 html 网站

www.myweb.com/app应该显示ci网站的前端

www.myweb.com/admin 它应该像现在一样工作

我试过的是:

我在google/stack overflow上搜索,发现了一些.htaccess解决方案,

<IfModule mod_rewrite.c>
     RewriteEngine On

     RewriteBase /
     RewriteCond %{HTTP_HOST} ^(www\.)?myweb\.com$ [NC]
     RewriteRule !^website/ /website%{REQUEST_URI} [L,NC]
     #above line works good and show website from website folder

     RewriteCond %{HTTP_HOST} ^(www\.)?myweb\.com$ [NC]
     RewriteRule (.*)/ %{REQUEST_URI} [L,NC]
     #when i add these two line website shows 500 internal server error

     #Other CI htaccess code to remove index.php
</IfModule>

你能建议我解决方案吗?谢谢

【问题讨论】:

  • 你把它全部倒过来了。这不是您应该通过重写来解决的问题。第一次正确创建您的网络结构。如果您希望 CI 在 /app 中,则创建一个文件夹 /app 并将其放在那里。将/website 文件移动到主文档根目录。这就是我要做的,而不是不必要的重写。
  • @PanamaJack:如果我将我的应用程序移动到 /app 中,它也会更改我的管理 url(www.myweb.com/adminwww.myweb.com/app/admin),我不希望这样。
  • 然后使用别名将 admin 映射到 /app/admin。 httpd.apache.org/docs/2.2/mod/mod_alias.html#alias 顺便说一句,出于安全目的,我从不希望公众可以轻松访问我的管理目录。 /admin 是一个简单的目标。这使您更容易受到攻击或暴力破解您的管理页面。您应该更多地考虑安全性而不是便利性。
  • @PanamaJack:感谢您的安全建议,我一直很照顾它,在这个 web 应用程序中,我没有超级用户的管理员,他们只是注册用户。谁可以在应用中添加 cmets

标签: apache .htaccess codeigniter redirect routes


【解决方案1】:

我找到了我的问题的解决方案,我想把它分享给大家

<IfModule mod_rewrite.c>
    RewriteEngine On

    # If the requested URI is empty...
    RewriteCond %{REQUEST_URI} !^$

    # ...then redirect to the "website" folder
    ^/?$ /website/ [L] //or[L,R] if you want to redirect

    # Otherwise rewrite the base
    RewriteBase /

    # If the request is not a folder or a file, redirects to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>

我将我的资产与 codeigniter 资产合并(更正了几条 css 和 js 路径),所以它对我有用。

希望它对某人有用。

从这篇文章中得到帮助:.htaccess - When there is no requested URI

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-17
    • 2013-07-10
    • 2021-07-19
    • 2017-08-17
    • 2014-06-01
    • 1970-01-01
    相关资源
    最近更新 更多