Codeigniter URL trailing slash breaks in subfolder

烈酒焚心 提交于 2019-12-23 05:30:15

问题


I have a site based on Codeigniter, and when I'm in the default directory, www.removed.com... I have no problem. If I go here: www.removed.com/ It works just fine.

The problem is when I navigate to the /admin folder (which is really just application/controllers/admin/dashboard.php..... routed correctly in the routes config.

If I add a trailing slash to the end of anything under admin (like removed.com/admin/users/), the whole site breaks and the url tries inserting a url relative to my root: http://www.removed.com/home/accountName/public_html/skc/admin/users

Any ideas?

Here's my .htaccess

<IfModule mod_rewrite.c>

RewriteEngine On

### Canonicalize codeigniter URLs

# If your default controller is something other than
# "welcome" you should probably change this
# RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
# RewriteRule ^(.*)/index/?$ $1 [L,R=301]

# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]

# Enforce www
# If you have subdomains, you can add them to 
# the list using the "|" (OR) regex operator
# RewriteCond %{HTTP_HOST} !^(www|subdomain) [NC]
# RewriteRule ^(.*)$ http://www.domain.tld/$1 [L,R=301]

# Enforce NO www
# RewriteCond %{HTTP_HOST} ^www\.removed\.com [NC]
# RewriteRule (.*) http://removed.com/$1 [R=301,L]

###

# Removes access to the system folder by users.
# Additionally this will allow you to create a System.php controller,
# previously this would not have been possible.
# 'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^ci.*
RewriteRule ^(.*)$ index.php/$1 [L]

# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

</IfModule>


<IfModule !mod_rewrite.c>

# Without mod_rewrite, route 404's to the front controller
ErrorDocument 404 /index.php

</IfModule>

Here's the routes config (as requested):

$route['default_controller'] = "main";
$route['404_override'] = '';
$route['about'] = "main/about";
$route['news'] = "main/news";
$route['bikes'] = "main/bikes";
$route['gallery'] = "main/gallery";
$route['contact'] = "main/contact";

$route['admin'] = "admin/dashboard";
$route['admin/login'] = "admin/dashboard/login";
$route['admin/logout'] = "admin/dashboard/logout";
$route['admin/users/view-user'] = "admin/users/view_user";
$route['admin/users/view-user/:num'] = "admin/users/view_user";

回答1:


Change this condition to exclude the admin directory:

RewriteCond %{REQUEST_URI} ^ci.*

Additionally, you're currently using a blacklist approach here, where a whitelist one would be more effective. So, if you for example have all your images, css and javascript files in an 'assets' directory, if we assume that this is everything that needs to be accessible from the outside world, you should do this:

RewriteCond %{REQUEST_URI} !^(index\.php|assets\/.*\..*)


来源:https://stackoverflow.com/questions/13354265/codeigniter-url-trailing-slash-breaks-in-subfolder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!