HTAccess和Regex ......排除字符串?

时间:2012-10-22 05:45:48

标签: regex .htaccess codeigniter

我正在使用htaccess用codeigniter重写我的URL ...具体来说。我有两个应用程序,一个名为“前端”,另一个名为“admincp”。前端应用程序使用index.php并将$ application_folder更改为“frontend”。 admincp应用程序使用复制的index.php,但重命名为admin.php。 $ application_folder在此文件中更改为“admincp”。

基本上,我希望所有包含“admincp”的URi作为第一个uri段被重写为admin.php文件而不是index.php。然后,所有其他URL将像往常一样重写到index.php。

mysite.com/admin/members将转到admin.php / members mysite.com/articles/Some-Awesome-Articles将转到index.php / articles

我希望我已经解释得这么好......无论如何,这是我当前的htaccess文件,但它似乎不起作用。我收到内部服务器错误。

 # 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\.site\.com$ [NC]
    # RewriteRule ^(.*)$ http://site.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 ^admin/(.*)$ admin.php/$1
    RewriteRule ^(.*)$ index.php/$1

</IfModule>

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

# 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\.site\.com$ [NC] # RewriteRule ^(.*)$ http://site.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 ^admin/(.*)$ admin.php/$1 RewriteRule ^(.*)$ index.php/$1 </IfModule> # If Mod_ewrite is NOT installed go to index.php <IfModule !mod_rewrite.c> ErrorDocument 404 index.php </IfModule>

2 个答案:

答案 0 :(得分:0)

您拥有的第一条规则将重写admin.php本身,看起来您最终会遇到类似/index.php/admin.php的内容。此外,检查!-f!-d 的2个条件仅适用于RewriteRule ^admin/(.*)$ admin.php/$1规则。重写条件仅在重写规则后立即应用于单个条件。因此,您的RewriteRule ^(.*)$ index.php/$1规则没有任何条件,并将盲目地重写每个URI。你也没有标志,所以重写一直持续到最后(从而破坏你以前做过的任何重写)。

您需要重新排列规则:

# rewrite if the request starts with `/admin/`
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^admin/(.*)$ admin.php/$1 [L]

# not even sure why you need this...
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteCond %{REQUEST_FILENAME} !admin.php
RewriteRule (.*)\.php$ index.php/$1 [L]

# everything else
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

答案 1 :(得分:0)

按照描述编写RewriteRule,将其放在全部捕获规则之前,然后以[L]结尾,替换为Last。它将跳过处理其余部分,将该规则视为最后一条。

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

Docs