1

I have a question about removing the ending of a file in the url bar (the .something like .php, .html, .js, .asp part). My goal is such that whenever somebody is typing in the url something like www.123.com/a where the file is, let's say a php file( so it's a.php ) so that people can type in www.123.com/a instead of www.123.com/a.php. How do I fix this?

Is there some unique way to save files so that they are saved and can be called via the url just by its name? In my case, if I call www.123.com/a it will say "object not found error" but it will work for www.123.com/a.php.

How do I hide the .something part of a file in the url bar?

Additional notes, I use notepad so whenever I save I type in a.php as save as for PHP or a.js for javascript files.

4

4 回答 4

6

通常这是通过以下方式完成的.htaccess

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.*) $1.php [QSA]
于 2012-12-25T01:26:39.073 回答
3

使用.htaccess!

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

https://stackoverflow.com/a/10469791/1533203

http://eisabainyo.net/weblog/2007/08/19/removing-file-extension-via-htaccess/

于 2012-12-25T01:28:32.210 回答
2

我该如何解决?是否有一些独特的方法来保存文件,以便它们被保存并可以通过 url 仅通过其名称调用?

不。

实际发生的是我们让 Web 服务器在请求到达 PHP 之前重新编写请求。

所以用户可能会输入(或被发送到):

http://stackoverflow.com/questions/14026969/remove-url-file-ending-php-js-asp-etc

Apache(或您正在使用的任何 Web 服务器)然后会将其重写为:

http://stackoverflow.com/index.php?page=questions&id=14026969&title=remove-url-file-ending-php-js-asp-etc

我们可以从中处理它。

于 2012-12-25T01:27:29.243 回答
0

如果 repo 文件夹中存在机器人,则创建 .htaccess 文件并将该代码添加到其中

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
于 2021-06-23T12:37:05.780 回答