为什么我的 URL 在使用 CodeIgniter 框架时只能在目录前面带有问号的情况下使用?

发布于 2024-10-04 01:22:37 字数 1937 浏览 0 评论 0原文

我在 PHP 框架 CodeIgniter 的 .htaccess 中使用以下代码。

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /

### 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 [NC]
#RewriteRule ^(.*)$ http://domain.tld/$1 [L,R=301]

###

# 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} ^system.*
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]

如果我输入网址 http://linuxservers.org/articles 文章页面不会加载(索引做)。但是,如果我输入 linuxservers.org/?articles 或 /?/articles,页面就会加载。我不明白。

除此之外,如果我将上面代码中的最后一行更改为

    RewriteRule ^(.*)$ index\.php/?$1 [L]

OR

    RewriteRule ^(.*)$ index\.php?/$1 [L]

(将 /articles 重定向到 /?articles 或 /?/articles),则会加载页面。 index.php/articles 也会加载页面,这是 CodeIgnitor 的默认格式。

希望这是有道理的,如果没有,请问我问题。期待您的意见。

I'm using the following code for my .htaccess for the PHP framework CodeIgniter.

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteBase /

### 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 [NC]
#RewriteRule ^(.*)$ http://domain.tld/$1 [L,R=301]

###

# 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} ^system.*
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]

If I type in the url http://linuxservers.org/articles The articles page does not load (the index does). However, if I type in linuxservers.org/?articles or /?/articles the page will load. I don't get it.

In addition to that, if I change the last line in the code above to

    RewriteRule ^(.*)$ index\.php/?$1 [L]

OR

    RewriteRule ^(.*)$ index\.php?/$1 [L]

(which redirects /articles to either /?articles or /?/articles, the page loads.
index.php/articles loads the page as well, which is the default format for CodeIgnitor.

Hopefully that made sense, if not, ask me questions. Looking forward to your input.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮世清欢 2024-10-11 01:22:37

您从哪里获取 .htaccess 文件? CodeIgniter wiki 的最后一行为:

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

这是您的工作选项之一。

Where did you get your .htaccess file from? The one on the CodeIgniter wiki has the last line as:

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

Which is one of your working options.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文