繁体   English   中英

我怎样才能让我的网站的访问者访问我的网页,无需添加页面扩展

[英]How can I make my site’s visitors access my web pages without adding the page extension

我是初学者,我刚刚主持了我的第一个网站; 我现在面临的问题是如何让我的网站访问者访问我的网站上的页面,即使没有添加页面扩展名。

示例:www.example.com/services而不是www.example.com/services.php

您可以使用url rewrite在未指定扩展名时附加扩展名。 或者您可以构建一个路由系统来将特定的uri链接到特定的资源( 此处为快速示例 )。 后者引入了很多复杂性,但提供了更多的控制。 例如,这里是基于我构建的自定义路由系统的基本伪代码:

$routes->addStaticRoute(
    /* Pattern */ '/home/myPage',
    /* Params  */ null,
    /* File    */ 'path/to/myPage.php'
);

所有请求都自动转到index.php,我的路由器将url请求转换为实际的资源路由。 使用上述静态路由,如果用户请求http://mySite/home/myPage ,路由器将使用路径wwwroot/path/to/myPage.php的静态文件进行响应。

静态路由的另一个例子:

$routes->addStaticRoute(
    /* Pattern */ '/home/{fileName}.{extension}',
    /* Params  */ ['fileName' => 'index', 'extension' => 'php'],
    /* File    */ 'path/to/{fileName}.{extension}'
);

如果用户请求http://mySite/home ,路由器将使用默认的wwwroot/path/to/index.php响应。 此外,如果他们请求http://mySite/home/page5 ,路由器将响应(如果存在) wwwroot/path/to/page5.php

路由有点像一个先进的概念,这些是一些过于简单的例子,但我希望这能让你开始。

编辑.htaccess文件并添加以下内容

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

它会完成

这是通常的解决方案:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule .* $0.php [L]

但是如果你想使用$_SERVER['PATH_INFO'])来允许:

http://my.domain/script/a/b
# instead of 
http://my.domain/script.php/a/b

......你需要这样的东西:

# specifix scripts:
RewriteRule ^(script1|script2)/(.*) /$1.php/$2 [L]
# or more general, but with issues for sub-directotries
RewriteRule ^([^./]+)/(.*) $1.php/$2 [L]

# final rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule .* $0.php [L]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM