本文介绍了URL重写,除去参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我在一个网站工作,所有的网页实际上是index.php文件+是分析了名参数并加载相应的模板和内容。
主页网址是:
http://www.some_site.com/?page=homepage


I'm working on a site where all the pages are actually index.php + a 'name' parameter that is analyzedand loads the appropriate template and content.
the homepage url is:
http://www.some_site.com/?page=homepage

1 有人问我变的主页网址:。
http://www.some_site.com
我可以使用URL rewite和htaccess的为,如果是这样,我应该怎么写呢?

1. i was asked to "change" the homepage url to:
http://www.some_site.com
can i use url rewite and htaccess for that and if so, what should i write there?

工作在我的本地机器,我想这code(改写模式启用):

working on my local machine, i tried this code (mode rewrite is enabled):

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteRule /index.php /index.php?page=homepage
</IfModule>

我仍然需要'名字'参数可用于当然的PHP code,这样我就可以加载模板和CSS文件。

i would still need the 'name' parameter to be available to the php code of course, so i can load thetemplate and css files.

2 这将是很好的其他网页(不是主页)从(例如)转化
http://www.some_site.com/?page=products
到:
http://www.some_site.com/products
这是不太重要的。

2. it would be nice for other pages (not homepage) to be converted from (example)
http://www.some_site.com/?page=products
to:
http://www.some_site.com/products
this is less crucial.

感谢名单提前有一个愉快的一天: - )

thanx in advance and have a nice day :-)

推荐答案

您不需要重写规则可言。只要改变你的index.php文件显示网页的时候没有页面可变的。

You don't need a rewrite rule at all. Just change your index.php file to show the homepage when there is no page variable at all.

if (!isset($_GET['page'])) {
    $_GET['page'] = 'homepage';
}

有关教育目的,重写规则:

For educational purposes, the rewrite rule:

RewriteRule /$ index.php?page=homepage [L]

也就是说,URI匹配只是斜线(URI中的URL的域名之后开始)。该 $ 表示,应该有斜线后没有字符。

That is, the URI to match is just the slash (the URI starts after your domain in the URL). The $ means that there should be no characters after the slash.

至于由只信的产品和这样的,假设一个字:

As for products and such, assuming single words made of only letters:

RewriteRule /([a-zA-Z]+)$ index.php?page=$1 [L]

这篇关于URL重写,除去参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 19:06