mod_rewrite 删除 .php 但仍然提供 .php 文件? [英] mod_rewrite to remove .php but still serve the .php file?

查看:22
本文介绍了mod_rewrite 删除 .php 但仍然提供 .php 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是想用 mod_rewrite 做一个简单的事情.我有一个使用 .php 文件的站点,我想将它们重写为更清晰的 URL,并删除 .php.因此,文件将是 www.mysite.com/contact 等.

I just wanted to do a simple thing with mod_rewrite. I have a site which uses .php files, and I wanted to rewrite those to cleaner URLs, and remove the .php. So, files would be www.mysite.com/contact and so on.

这确实像我想要的那样工作,但我原以为它仍然会为我的 contact.php 文件提供服务,但只是向用户显示他们在/contact 而不是 contact.php.但是,它正在寻找一个名为 contact 的文件,但该文件不存在.

This does work how I wanted, but I had expected that it would still serve my contact.php file, but just show the user that they were at /contact rather than contact.php. But, it is looking for a file just called contact, which, is not there.

那么,我需要做什么,仍然使用我的 contact.php 文件,但将用户的 URL 重写为/contact ?

So, what so I need to do, do still use my contact.php file, but rewrite the URL for the user to /contact ?

这是我正在使用的:

SetEnv APPLICATION_ENV development
RewriteEngine on
RewriteBase /

# Always use www.
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]

# Change urlpath.php to urlpath
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
RewriteRule ^(.*)\.php$ http://www.mysite.com/$1 [L,R=301]

推荐答案

对于这个解决方案,我遵循了以下规则:

For this solution, I have followed the following rules:

  1. 如果用户尝试加载 /something.php,他们应该从外部重定向到 /something.
  2. 如果用户尝试加载 /something,那么他们应该在内部重定向到 /something.php.
  3. 如果用户向 URL 传递了任何查询字符串参数,则这些参数应通过重定向保留.
  4. 如果用户尝试加载文件系统中实际存在的不同文件(样式表、图像等),则应按原样加载.
  1. If the user tries to load /something.php they should be externally redirected to /something.
  2. If the user tries to load /something then they should be internally redirected to /something.php.
  3. If the user passed any query string parameters to the URL then these should be preserved through the redirects.
  4. If the user tries to load a different file which really exists on the filesystem (a stylesheet, image etc) then this should be loaded as is.

这是最后一组 mod_rewrite 魔法:

And here's the final set of mod_rewrite magic:

RewriteEngine on
RewriteBase /

## Always use www.
RewriteCond %{HTTP_HOST} ^mysite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]

# Change urlpath.php to urlpath
## Only perform this rule if we're on the expected domain
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
## Don't perform this rule if we've already been redirected internally
RewriteCond %{QUERY_STRING} !internal=1 [NC]
## Redirect the user externally to the non PHP URL
RewriteRule ^(.*)\.php$ $1 [L,R=301]

# if the user requests /something we need to serve the php version if it exists

## Only perform this rule if we're on the expected domain
RewriteCond %{HTTP_HOST} ^www\.mysite\.com$ [NC]
## Perform this rule only if a file with this name does not exist
RewriteCond %{REQUEST_FILENAME} !-f
## Perform this rule if the requested file doesn't end with '.php'
RewriteCond %{REQUEST_FILENAME} !\.php$ [NC]
## Only perform this rule if we're not requesting the index page
RewriteCond %{REQUEST_URI} !^/$
## Finally, rewrite the URL internally, passing through the user's query string
## using the [qsa] flag along with an 'internal=1' identifier so that our first
## RewriteRule knows we've already redirected once.
RewriteRule ^(.*)$ $1.php?internal=1 [L, QSA]

这篇关于mod_rewrite 删除 .php 但仍然提供 .php 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆