[英]Use htaccess to remove trailing slashes, index.php, and still use relative paths
I am trying to write an .htaccess file that accomplishes the following goals: 我正在尝试编写一个实现以下目标的.htaccess文件:
Some example URLS (display URL = actual location): 一些示例URL(显示URL =实际位置):
And here's what I have so far in the folder ABOVE admin. 这是到目前为止我在admin文件夹中的内容。 It removes the php extension while allowing me to access any existing files, but it also strips everything after .php and doesn't remove trailing slashes.
它删除了php扩展名,同时允许我访问任何现有文件,但是它也删除了.php之后的所有内容,并且不删除结尾的斜杠。
RewriteEngine On
# Redirect php filename externally
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L,QSA]
# change 302 to 301 on live for perm redirect
# Redirect php filename internally
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L,QSA]
# Rewrite Admin URLS (after specififcally looking for files)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule admin/. admin/index.php [L,QSA]
# index.php looks at $_SERVER['REQUEST_URI'] to decide what to do next
# No Directory Listings
Options -Indexes
My biggest concern is being able to include files (.php, .inc, .css, .js) with relative paths and not having to use absolute paths if I can help it. 我最大的担心是能够包含具有相对路径的文件(.php,.inc,.css,.js),如果可以帮助的话,不必使用绝对路径。
I generally prefer to go with a framework that provides routing capabilities instead of the nightmare of all those Rewrite rules for folders/files. 我通常更喜欢使用提供路由功能的框架,而不是所有针对文件夹/文件的重写规则的噩梦。 There are a slew of PHP frameworks that support this easily, such as Silex , Symfony , or Laravel , to name a few.
有很多PHP框架可以轻松地支持此功能,例如Silex , Symfony或Laravel等等。
Another reason that doing the routing in the application is easier, is that it abstracts application routing from the webserver for the most part. 在应用程序中进行路由更容易的另一个原因是,它在很大程度上从Web服务器提取了应用程序路由。 For example, if you move to Nginx, you will have to refactor all of the above.
例如,如果您迁移到Nginx,则必须重构以上所有内容。
My .htaccess might look something like the following: 我的.htaccess可能类似于以下内容:
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirect to non-trailing slash
RewriteRule ^(.*)/$ /$1 [R=301,L]
# Block access to "hidden" directories whose names begin with a period.
RewriteRule "(^|/)\." - [F]
RewriteBase /
# Redirect requests that have no real file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php [QSA,L]
</IfModule>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.