如何重写 URL 以删除 index.php 并使用 codeigniter 强制小写?

发布于 2024-10-30 23:08:33 字数 378 浏览 1 评论 0原文

我在使用 .htaccess 同时执行这两项操作时遇到困难。

删除index.php 可以正常工作,但尝试添加强制小写字母会引发 500 错误。我正在使用代码点火器。任何帮助表示赞赏!

这是我的 .htaccess 代码:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1

RewriteMap  lc int:tolower
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule (.*) ${lc:$1} [R=301,L]

I’m having trouble doing both of these at once with .htaccess.

Removing index.php is working, but trying to add the force lowercase keeps throwing a 500 error. I am using codeigniter. Any help is appreciated!

here’s my .htaccess code:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1

RewriteMap  lc int:tolower
RewriteCond %{REQUEST_URI} [A-Z]
RewriteRule (.*) ${lc:$1} [R=301,L]

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

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

发布评论

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

评论(2

活雷疯 2024-11-06 23:08:34

那是因为这是不可能的,让我解释一下。

  1. 删除index.php仍然会将所有请求发送到index.php/yadda/yadda,因此请求的uri中仍然包含index.php,即使您没有看到它或将其添加到url中。
  2. 重定向(由第二个重写规则触发)使索引部分无效,因此您将获得 mysite.com/index.php/lowercase/

但除此之外,此 RewriteMap 只能在您的 Httpd.conf 文件中声明,您可以在那里声明它with:

RewriteMap  lc int:tolower

然后在你的 .htaccess 文件中使用你的变量 lc ,但同样只有两者之一会获胜,你不能同时拥有两者。

您要么获得小写的 URL,要么让网站在不使用 index.php 的情况下正常工作,因为由于每个人所做的事情的性质,它们总是会发生冲突。

实际上,我能看到它发生的唯一方法是在 php 中,如下所示:

$this->load->helper('url');
$your_URL = uri_string();
preg_match_all('/[A-Z]/', $your_URL, $match) ;
$total_count = count($match [0]);
if($total_count > 0){
    $new_line = strtolower($your_URL);
    redirect($new_line);
}

我会将其放入类的主要构造中,我希望这对您有所帮助。

That is because it is impossible, let me explain.

  1. Removing the index.php still sends all requests to index.php/yadda/yadda so the requested uri still has the index.php in it even though you do not see it or add it into the url.
  2. The redirect (triggered by the 2nd rewrite rule) nulls the index section so you would then get mysite.com/index.php/lowercase/

BUT beyond all this RewriteMap can only be declared in your Httpd.conf file, you would declare it there with:

RewriteMap  lc int:tolower

Then in your .htaccess file use your variable lc, but then again only one of the two will win you can't have both.

You will either get the URL lowercase or have the site working without using the index.php because they are always going to conflict because of the nature of what each one does.

Really the only way I can see it happening is in php, like so:

$this->load->helper('url');
$your_URL = uri_string();
preg_match_all('/[A-Z]/', $your_URL, $match) ;
$total_count = count($match [0]);
if($total_count > 0){
    $new_line = strtolower($your_URL);
    redirect($new_line);
}

This I would put into the main construct of your classes, I hope this helps you.

挽清梦 2024-11-06 23:08:33

如果您只是想强制使用小写,则建议的 php 代码不起作用。下面是正确的 php 代码。

$url = $_SERVER['REQUEST_URI'];
$pattern = '/([A-Z]+)/';

if(preg_match($pattern, $url)) {
    $new_url = strtolower($url);

    Header( 'HTTP/1.1 301 Moved Permanently' );
    Header( 'Location: ' . $new_url );
    exit;
}

// your code here

此处找到讨论 强制小写 .htaccess

The php code suggested does not work if you are just trying to force to lowercase. Below is the correct php code.

$url = $_SERVER['REQUEST_URI'];
$pattern = '/([A-Z]+)/';

if(preg_match($pattern, $url)) {
    $new_url = strtolower($url);

    Header( 'HTTP/1.1 301 Moved Permanently' );
    Header( 'Location: ' . $new_url );
    exit;
}

// your code here

Discussion Found Here Force lowercase .htaccess

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