问题描述
我试图强制拆除或使用尾随斜线到我的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 $下执行斜杠?
您可以处理与此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加斜杠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!