使用的.htaccess查询字符串重写重写、字符串、htaccess

2023-09-02 09:39:17 作者:喝醉以后,才会走的猫步

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

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.