简体   繁体   English

Apache2重写,删除文件扩展名

[英]Apache2 Rewrite, remove file extensions

Question

With an apache rewrite, when a user inputs: 当用户输入以下内容时,使用apache重写:

example.com/home/
OR
example.com/home

The website will send the user to: 该网站会将用户发送至:

example.com/home.php

but the url will continue to be: 但网址将继续为:

example.com/home/

My broken solution 我破碎的解决方案

My current code in .htaccess is: 我目前在.htaccess中的代码是:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(home/)$ home.php [NC,L]

but it seems to redirect the user to "home.php.php.php.php" and so on. 但似乎将用户重定向到“ home.php.php.php.php”,依此类推。

First of all, it is a bad idea to have both example.com/home/ and example.com/home leading to same content (duplicate content is not appreciate by search engines like Google). 首先,将example.com/home/example.com/homeexample.com/home/相同的内容是个主意(重复的内容不会受到Google等搜索引擎的青睐)。

Then, you have to disable MultiViews to get the behaviour you want. 然后,您必须禁用MultiViews才能获得所需的行为。

You can try with this code 您可以尝试使用此代码

Options -MultiViews
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^home/$ /home.php [NC,L] 
# or ^home$ if you don't want it with trailing slash

Try this 尝试这个

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

You can access any PHP file without php extension :) 您可以访问任何没有php扩展名的PHP文件:)

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

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