简体   繁体   English

使用htaccess删除结尾的斜杠,index.php,并仍然使用相对路径

[英]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文件:

  • Strip trailing slash at the end of all URLS 在所有URL的末尾加上斜杠
  • Drop the file name for index.php 删除index.php的文件名
  • Drop the .php extension for everything that isn't the index 删除所有不是索引的.php扩展名
  • Keep all trailing data in URLs to be processed by index.php 保留URL中的所有尾随数据以供index.php处理
  • Use relative URLs, without a link to the site root since the .htaccess only affects a subfolder (/admin as in the examples below) 使用相对URL,而不链接到站点根目录,因为.htaccess仅影响子文件夹(如以下示例中的/ admin)

Some example URLS (display URL = actual location): 一些示例URL(显示URL =实际位置):

  • /admin = /admin/index.php / admin = /admin/index.php
  • /admin/pagename = /admin/pagename.php / admin / pagename = /admin/pagename.php
  • /admin/directory = /admin/directory/index.php / admin / directory = /admin/directory/index.php
  • /admin/one/two/three = /admin/index.php where I can parse $_SERVER['REQUEST_URI'] with PHP to get the variables one, two, and three / admin / one / two / three = /admin/index.php,在这里我可以用PHP解析$ _SERVER ['REQUEST_URI']以获取变量一,二和三

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框架可以轻松地支持此功能,例如SilexSymfonyLaravel等等。

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.

相关问题 使用.htaccess从网址中删除尾随/index.php - Remove trailing /index.php from url using .htaccess 如何将所有请求路由到index.php,将不存在的文件路由到错误页面,使用漂亮的URL以及添加斜杠? - How to route all requests to index.php, route non-existing files to error page, use pretty URLs, and add trailing slashes? 重写域,但仍使用index.php - Rewrite domain, but still use index.php 5 .htaccess重写:强制HTTPS,删除index.php,删除.php,强制www,强制尾随斜杠 - 5 .htaccess Rewrites: Force HTTPS, Remove index.php, Remove .php, Force www, Force Trailing Slash Heroku对所有不存在的文件路径使用index.php - Heroku use index.php for all not existent file paths 删除URL中的双斜杠并删除index.php Apache - Remove double Slashes in URL and remove index.php Apache 如何使用htaccess将root和/index.php显示为/ news? - How to use htaccess to make root and /index.php display as /news? 使用.htaccess强制在特定页面(index.php)上进行不同的缓存 - Use .htaccess to force caching differently on specific page (index.php) 清洁网址www。 非,删除index.php,删除.php扩展名,使用HTACCESS添加结尾斜杠 - Clean URLS www. to non, remove index.php, remove .php extention, add trailing slash using HTACCESS htaccess-添加www并删除index.php - htaccess - add www & remove index.php
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM