简体   繁体   English

如何重写某个目录的URL?

[英]How can I rewrite URL's of a certain directory?

Ok, vague question title, I know. 好的,我知道模糊的标题。 Wasn't sure how to summarize this in one line. 不确定如何将其总结为一行。 I have two applications in my CodeIgniter installation: frontend and admin. 我的CodeIgniter安装中有两个应用程序:frontend和admin。 There are two front controllers for each that set to the correct application folder. 每个都有两个前端控制器,它们设置为正确的应用程序文件夹。 Currently, I use index.php/home for the home page of the frontend and admin.php/home for the home page of the admin panel. 当前,我将index.php / home用作前端的主页,并将admin.php / home用作管理面板的主页。

I finally got around to removing index.php from the URL of the frontend. 我终于设法从前端的URL中删除index.php。 For the admin cp, I would like to use example.com/admin instead of example.com/admin.php. 对于管理员cp,我想使用example.com/admin而不是example.com/admin.php。 So I basically need to rewrite any uri that has admin as the first segment to admin.php. 因此,我基本上需要将具有admin作为第一段的uri重写为admin.php。 I figured that adding RewriteRule ^admin/(.*)$ admin.php/$1 to my htaccess would do the trick, but apparently it doesn't... Not completely anyway. 我认为将RewriteRule ^admin/(.*)$ admin.php/$1到我的htaccess可以解决问题,但显然没有。 I get 404 Page Not Found for every single page in my admin cp. 我在管理cp中的每个页面都得到404页面未找到。 What am I doing wrong? 我究竟做错了什么?

# Deny OR Allow Folder Indexes.
# Since we disable access to PHP files you 
# can leave this on without worries. 
# OR better yet, create a .htaccess file in
# the dir you want to allow browsing and
# set it to +Indexes
Options -Indexes

Options +FollowSymLinks

# Set the default file for indexes
DirectoryIndex index.php

<IfModule mod_rewrite.c>
    # mod_rewrite rules
    RewriteEngine on

    # The RewriteBase of the system (if you are using this sytem in a sub-folder).
    # RewriteBase /CodeIgniter_1.6.3/

    # This will make the site only accessible without the "www." 
    # (which will keep the subdomain-sensive config file happy)
    # If you want the site to be accessed WITH the "www." 
    # comment-out the following two lines.
    # RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
    # RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

    # If a controler can't be found - then issue a 404 error from PHP
    # Error messages (via the "error" plugin)
    # ErrorDocument 403 /index.php/403/
    # ErrorDocument 404 /index.php/404/
    # ErrorDocument 500 /index.php/500/

    # Deny any people (or bots) from the following sites: (to stop spam comments)
    # RewriteCond %{HTTP_REFERER} nienschanz\.ru [NC,OR]
    # RewriteCond %{HTTP_REFERER} porn\.com
    # RewriteRule .* - [F]
    # Note: if you are having trouble from a certain URL just 
    # add it above to forbide all visitors from that site.

    # You can also uncomment this if you know the IP:
    # Deny from 192.168.1.1

    # If the file is NOT the index.php file
    RewriteCond %{REQUEST_FILENAME} !index.php
    # Hide all PHP files so none can be accessed by HTTP
    RewriteRule (.*)\.php$ index.php/$1

    # If the file/dir is NOT real go to index
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php/$1 [QSA,L]
    RewriteRule ^admin/(.*)$ admin.php/$1

</IfModule>

# If Mod_ewrite is NOT installed go to index.php
<IfModule !mod_rewrite.c>
    ErrorDocument 404 index.php
</IfModule> 

You need to add your rule before you index.php routing rule. 您需要 index.php路由规则之前添加规则。 It's because ^(.*)$ is matching admin/whatever , so your admin rule will never get executed. 这是因为^(.*)$admin/whatever匹配,所以您的管理规则将永远不会执行。 You should also add -Multiviews to your options so it looks like this: 您还应该在选项中添加-Multiviews ,因此它看起来像这样:

Options +FollowSymLinks -Multiviews

Then right under RewriteEngine On , add your rule: 然后在RewriteEngine On ,添加您的规则:

RewriteRule ^admin/(.*)$ admin.php/$1 [L]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM