mod_rewrite的去除的.php,但仍服务于PHP文件? [英] mod_rewrite to remove .php but still serve the .php file?

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

问题描述

我只想做一个简单的事情与mod_rewrite的。我有一个使用PHP文件的网站,我想重写这些清洁的URL,并取出的.php。因此,文件将被www.mysite.com/contact~~V等

这不工作,我怎么想,但我此前的预期,它仍然会成为我的contact.php文件,而只是表明他们在/接触,而不是contact.php用户。但是,它正在寻找刚打电话联系人文件,这是不存在的。

那么,是什么,所以我需要做,还是用我的contact.php文件,但重写的URL为用户/联系人?

下面是我使用的:

  SETENV APPLICATION_ENV发展
RewriteEngine叙述上
的RewriteBase /#始终使用WWW。
的RewriteCond%{HTTP_HOST} ^ mysite的\\ .COM $ [NC]
重写规则^(。*)$ http://www.mysite.com/$1 [L,R = 301]#更改urlpath.php到urlpath
的RewriteCond%{HTTP_HOST} ^ WWW \\ .mysite \\ .COM $ [NC]
重写规则^(。*)\\。PHP $ http://www.mysite.com/$1 [L,R = 301]


解决方案

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


  1. 如果用户尝试加载 /something.php 他们应该在外部重定向到 /东西

  2. 如果用户尝试加载 /东西那么就应该在内部重定向到 /something.php

  3. 如果用户传递的任何查询字符串参数的URL那么这些应该通过重定向pserved $ P $。

  4. 如果用户尝试加载它确实存在于文件系统(样式表,图像等)不同的文件,则该应加载原样。

和这里的最后一组神奇的mod_rewrite的:

  RewriteEngine叙述上
的RewriteBase /##始终使用WWW。
的RewriteCond%{HTTP_HOST} ^ mysite的\\ .COM $ [NC]
重写规则^(。*)$ http://www.mysite.com/$1 [L,R = 301]#更改urlpath.php到urlpath
##只有当我们的预期域中执行这一规则
的RewriteCond%{HTTP_HOST} ^ WWW \\ .mysite \\ .COM $ [NC]
如果我们已经在内部重定向##,请不要执行这个规则
的RewriteCond%{QUERY_STRING}!内部= 1 [NC]
##外部将用户重定向到非PHP网址
重写规则^(。*)\\。PHP $ $ 1 [L,R = 301]#如果用户请求/东西需要我们服务的PHP版本(如果存在)##只有当我们的预期域中执行这一规则
的RewriteCond%{HTTP_HOST} ^ WWW \\ .mysite \\ .COM $ [NC]
只有具有此名称的文件不存在##执行这一规则
的RewriteCond%{} REQUEST_FILENAME!-f
如果请求的文件不符合的.php结束##执行这一规则
的RewriteCond%{} REQUEST_FILENAME!\\ PHP $ [NC]
##只有当我们不要求索引页面执行这一规则
的RewriteCond%{REQUEST_URI}!^ / $
##最后,重写URL内部,通过用户的查询字符串
##使用[QSA]标志和内部= 1'标识符一起,使我们的第一次
##重写规则知道我们已经重定向一次。
重写规则^(。*)$ $ 1.PHP?内部= 1 [L,QSA]

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.

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.

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

Here is what I am using:

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. 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.

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天全站免登陆