2

我希望用这个 .htaccess 文件完成两件事。

1) 所有对 .html 的请求都指向 .php 例如。用户访问:http ://www.mywebsite.com/contact.html 浏览器加载:http ://www.mywebsite.com/contact.php

2)请求http://www.mywebsite.com/contact将加载contact.php(这应该适用于所有页面,而不仅仅是联系页面。

这是我的 .htaccess 文件。

Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L]

老实说,我不知道这些在做什么。我将它们从我读过的杂乱无章的文章中混合在一起。任何帮助,将不胜感激。

4

3 回答 3

4

尝试这样的事情。关注评论线程以了解安全隐患,但这是您要求做的

RewriteEngine On
RewriteBase /

# Redirect HTML to PHP
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*).html$ $1.php [L]

# Otherwise, try PHP
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^(.*)$ $1.php [L]

# Lastly, fallback to error page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . 404.php [L]
于 2013-03-19T20:37:15.763 回答
0

I need to explain something to you about htaccess file roles;

In this example, htaccess will try and match patterns, as you need in your example;

If a pattern is matched, then some action will happen;

you mark with () all the data than you need to be extracted from the url

For each rule, the first () will have the $1 id, the seccond () will have $2 and so on

if $1 is 'help' it will load the 'helpme.php' file maybe, and not the 'help.php' file; it will load the file that you want it to be loaded;

using $1, $2 ... you can pass parameters and values to the real/translated url request

in my example, i wanted to always use the index.php file, you will use whatever file you need

Options +SymLinksIfOwnerMatch

RewriteEngine on

RewriteRule ^([a-zA-Z-]+)$ index.php?action=$1 [NC,L]

RewriteRule ^(member)-([0-9-]+)$ index.php?action=member&id=$2 [NC,L]

RewriteRule ^([a-zA-Z-]+)/([a-zA-Z-]+)-([0-9-]+)$ index.php?action=$1&saction=$2&sid=$3 [NC,L]

RewriteRule ^([a-zA-Z-]+)/([0-9-]+)$ index.php?action=$1&id=$2 [NC,L]

RewriteRule ^([0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/(.*).html$ index.php?action=details&id=$1&p1=$2&p2=$3&p3=$4 [NC,L]
于 2013-03-19T20:44:12.800 回答
0

如果您希望所有 .html 文件都指向 .php 文件,那么一种简单的方法是

RewriteRule ([\w\d\-_]+)(\.html)? $1.php [L]

这还允许您使用子目录

contact.html 会重定向到contact.php

/subdir/page.html 将重定向到 /subdir/page.php 等等

于 2013-03-19T20:46:03.950 回答