如何将/filename.php转换为/ filename /

时间:2013-05-22 07:25:51

标签: php url-rewriting

我似乎无法谷歌这个。我正在尝试构建我的网站,但我想指定文件(即/filename.php)并将它们转换为url和links中的/ filename /。我不确定这是url重写还是文件重定向或其他什么?我可以使用这个框架吗?

3 个答案:

答案 0 :(得分:1)

在htaccess文件中使用以下内容:

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

带有!-d的行检查它不是目录,带-f的行检查所请求的文件是否是.php文件。

Current URL: www.example.com/something.php 
Output  URL:www.example.com/something/

答案 1 :(得分:1)

这通常是通过Web服务器完成的,因此答案取决于您使用的是Apache,Nginx还是其他东西。以下是我使用Nginx的方法。

我的网站根目录中通常有一个名为controllers的目录。 controllers中的所有文件都对应于URL路径。例如:

/controllers/home.php ---> http://example.com/home
/controllers/about.php ---> http://example.com/about
/controllers/contact.php ---> http://example.com/contact

然后我使用Web服务器(Nginx)来引导流量(这是它构建的内容)。因此,如果Nginx收到http://example.com/home的请求,那么它会在controllers目录中查找名为home.php的文件。如果该文件存在,则Nginx将其提供。如果该文件不存在,则Nginx会提供一些后备页面。这是Nginx配置的样子:

server {

  listen 80;
  server_name example.com;

  root /srv/example;
  index index.php;

  rewrite ^/(.*)/$ /$1 permanent;

  location / {
    try_files $uri $uri/ /controllers$uri.php?$args;
    location ~ \.php$ {
      try_files $uri /index.php;
        fastcgi_split_path_info ^(.+\.php)(/.+)$;
        fastcgi_pass 127.0.0.1:9000;
        fastcgi_index index.php;
        include fastcgi_params;
      }
   }
}

答案 2 :(得分:0)

你可以为slug创建一个表.slug与文件名和目录相关联。

slu p表的结构应该是slugId | slug |文件名|路径

所有请求都转到您的Url类,该类根据slug-name()实际获取文件名

在网址上显示。您的网站htaccess应该在索引中移动所有请求。在你称之为

的索引上

urls的函数调用并根据slug要求值。