删除或CakePHP的2.x的基于Apache使用的.htaccess加斜杠 [英] Remove or add trailing slash from CakePHP 2.x on Apache using .htaccess

查看:226
本文介绍了删除或CakePHP的2.x的基于Apache使用的.htaccess加斜杠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图强制拆除或使用尾随斜线到我的CakePHP的2.x的应用程序中添加下列位于 /应用程序/ Web根目录中的.htaccess中。

I am trying to enforce the removal or add of trailing slashes to my CakePHP 2.x app using the following inside the .htaccess located at /app/webroot.

#<IfModule mod_rewrite.c>
#    RewriteEngine On
#    RewriteCond %{REQUEST_FILENAME} !-d
#    RewriteCond %{REQUEST_FILENAME} !-f
#    RewriteRule ^ index.php [L]
#</IfModule>


<IfModule mod_rewrite.c>
# Add trailing slash
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|)$
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
</IfModule>

以下为添加了结尾的斜线。

or the following for add the trailing slash.

RewriteCond %{REQUEST_URI} !(/$|\.) 
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]

但我不希望强制域,因为它可以在本地或子文件夹中运行。我该如何解决这个问题?所有我看了网上的文档似乎强制执行应用程序的完整URL。

But I don't want to enforce the domain as it could be running locally or inside a subfolder. How do I fix this? All the docs I've read online seem to enforce the FULL url of the app.

在,即使我用的是完整的URL它仍然无法正常工作的事实。有没有人成功地得到这个工作使用CakePHP?看来你必须通过index.php文件运行

为了澄清我正在寻找一个解决方案,添加和删除尾随斜线CakePHP中,而无需到c网址到.htaccess文件硬$ C $。

推荐答案

这是通常不是一个复杂的事情。

This is usually not a complicated thing.

这个怎么样?

<IfModule mod_rewrite.c>
   RewriteEngine On

   # if this is an existing folder/file then leave
   RewriteCond %{REQUEST_FILENAME} -d [OR]
   RewriteCond %{REQUEST_FILENAME} -f
   RewriteRule . - [L]

   # if trailing slash then redirect to url without trailing slash
   RewriteRule ^/?(.+)/$ /$1 [R=301,L]

   # internal rewrite to controller/dispatcher index.php
   RewriteRule ^.*$ /index.php [L,QSA]
</IfModule>

如果这个人是不工作的,以及这可能是在你身边的一个问题。
你之前已经使用mod_rewrite?难道是启用?

If this one is not working, well this could be a problem on your side.
Did you already use mod_rewrite before ? Is it enabled ?

编辑:如果你的网站是不是在根文件夹( DOCUMENT_ROOT ),你有两个选择。你可以把你的htaccess的应用程序文件夹或根文件夹。

if your website is not in root folder (DOCUMENT_ROOT) you have 2 options. You could put your htaccess in application folder or in root folder.

我建议你把你的htaccess在你的应用程序文件夹(专用于您的网站只)。否则,code不会完全相同。这里的code在这种情况下:

I suggest you to put your htaccess in your application folder (dedicated for your website only). Otherwise, the code would not be exactly the same. Here's the code in this case:

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteBase /application/

   # if this is an existing folder/file then leave
   RewriteCond %{REQUEST_FILENAME} -d [OR]
   RewriteCond %{REQUEST_FILENAME} -f
   RewriteRule . - [L]

   # if trailing slash then redirect to url without trailing slash
   RewriteRule ^/?(.+)/$ $1 [R=301,L]

   # internal rewrite to controller/dispatcher index.php
   RewriteRule ^.*$ index.php [L,QSA]
</IfModule>


您的第二个问题:


Your second question:

另外这将是$ C $下执行斜杠?

Also what would be the code for ENFORCING a trailing slash?

您可以处理与此code(只有一行改变)

You could handle that with this code (only one line changed)

<IfModule mod_rewrite.c>
   RewriteEngine On
   RewriteBase /application/

   # if this is an existing folder/file then leave
   RewriteCond %{REQUEST_FILENAME} -d [OR]
   RewriteCond %{REQUEST_FILENAME} -f
   RewriteRule . - [L]

   # if no trailing slash then redirect to url with trailing slash
   RewriteRule ^/?(.+)([^/])$ $1$2/ [R=301,L]

   # internal rewrite to controller/dispatcher index.php
   RewriteRule ^.*$ index.php [L,QSA]
</IfModule>

这篇关于删除或CakePHP的2.x的基于Apache使用的.htaccess加斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆