重写网站中的网页网址

时间:2014-05-03 07:25:53

标签: php .htaccess

我创建了一个简单的网站,其中一些页面网址的格式为“* .php”。

我想将我的网址/home.php设为/home。我可以在.htaccess文件中执行此操作吗?

我在这里添加我的代码

<files campus_config.ini>
order allow,deny
deny from all
Allow from 127.0.0.1
</files>

<files student.ini>
order allow,deny
deny from all
Allow from 127.0.0.1
</files>

<FilesMatch (variables|mysql)\.php>
order allow,deny  
deny from all
</FilesMatch>

RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]

示例网址: localhost / test / campus / login.php(test是一个文件夹,campus是我的项目文件夹,.htaccess文件在我的项目文件夹中)

2 个答案:

答案 0 :(得分:0)

您正在寻找的内容称为URL重写。这通常由应用程序服务器(通常是Apache或IIS)(在Windows上)完成。 how to do it in Apache。在Windows上,它取决于所用IIS的版本(6-8)。

答案 1 :(得分:0)

将此行放在/test/campus/.htaccess

RewriteEngine on
RewriteBase /test/campus/

RewriteCond %{THE_REQUEST} /(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ %1 [R=301,L,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ $1.php [L]
相关问题