使用的.htaccess查询字符串重写 [英] Query string rewriting using .htaccess

查看:218
本文介绍了使用的.htaccess查询字符串重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图重写的搜索引擎优化目的的网址。

i am trying to rewrite a URL for SEO purpose.

旧网址是:

http://www.domain.net/index.php?p=beer

新的URL应该是:

The new URL should be:

http://www.domain.net/beer

我的code。在htaccess的是:

My Code in the .htaccess is:

RewriteRule ^([^/\.]+)/?$ index.php?p=$1

即使经过研究小时,我不知道这是为什么不工作:(

Even after hours of research, i have no clue why this is not working :(

下面是完整的.htaccess:

Here is the complete .htaccess:

RewriteEngine on 
RewriteCond %{HTTP_USER_AGENT} Teoma
RewriteRule ^.* - [F]
rewritecond %{HTTP_HOST} !^www\.domain\.net$ [NC]
rewriterule ^(.*)$ http://www\.domain\.net/$1 [R=301,L]

RewriteCond %{QUERY_STRING} ^p=uppic$
RewriteRule ^index\.php$ /? [L,R=301]

RewriteRule ^([^/\.]+)/?$ index.php?p=$1

# Pwd service

AuthType Basic
AuthName "Service"
AuthUserFile /xxxx/www/xxxxxx/xxxxx/xxxxxx/.htpasswd


<Files admin.php>
Require user xxxxxxx
</Files>

Options -Indexes

在此先感谢!

Thanks in advance!

我的最后一个问题,这个code是:

My final question to this code is:

RewriteRule ^([^/\.]+)/?$ index.php?p=$1

使工作:

http://www.domain.net/beer

和啤酒指的是页面:

http://www.domain.net/index.php?p=beer

这是伟大的!但是,如果我把一个/后面的啤酒,例如:

Which is great! But if i put a / behind beer, e.g.:

http://www.domain.net/beer/

我beer.php文件运行在另一条路径,所以没有CSS,图片,JS等也包括在内。任何想法如何解决这个问题不改变的HTML http://www.domain.net/style.css ...

推荐答案

如果你想捕捉的查询字符串的一部分,你必须使用的 的RewriteCond QUERY_STRING

If you want to capture part of the query string, you must use a RewriteCond with QUERY_STRING

RewriteEngine On
RewriteCond %{QUERY_STRING} p=(.+)
RewriteRule ^/?index.php$ /%1? [R,L]

这会将客户端重定向到新的URL http://www.domain.net/beer

This redirects the client to the new URL http://www.domain.net/beer.

这篇关于使用的.htaccess查询字符串重写的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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