.htaccess:如何只重写.php网址?

时间:2021-01-09 11:09:38

My rewriterule with a condition is working fine as below:

我的rewriterule有条件工作正常如下:

http://www.sitename.com/index.php?n=text redirects to http://www.sitename.com/pages/text

http://www.sitename.com/index.php?n=text重定向到http://www.sitename.com/pages/text

and the page renders properly, however, there is a problem that with the redirected URL the arguments are also added to the URL. So actually in address bar it looks like-

并且页面呈现正确,但是,存在一个问题,即重定向的URL中的参数也会添加到URL中。所以实际上在地址栏中它看起来像 -

http://www.sitename.com/pages/text?n=text

Could anyone help me on this? The htaccess code is given below.

有人可以帮我吗? htaccess代码如下。

RewriteCond %{QUERY_STRING} ^n=(.*)$
RewriteRule index.php http://www.sitename.com/pages/%1 [r=301,nc]

3 个解决方案

#1


You probably want to catch "index.php.*". Otherwise mod_rewrite only replaces the "index.php" part of the URL "index.php?n=text" with the new URL.

你可能想要捕获“index.php。*”。否则mod_rewrite只用新URL替换URL“index.php?n = text”的“index.php”部分。

#2


Guss,

From what you suggested, i reconstructed it as follows:

根据你的建议,我重建如下:

RewriteCond %{QUERY_STRING} ^n=(.*)$
RewriteRule index.php.* http://www.sitename.com/pages/%1 [r=301,nc]

This doesnt seem to be working either. Can you please elaborate on what you have said?

这似乎也没有工作。你能详细说明你所说的话吗?

thank you aditya

谢谢aditya

#3


don´t use the url in the rewrite rule, apache then sends a http 200 code and then the 301...

不要在重写规则中使用url,apache然后发送一个http 200代码然后发送301 ...

try sth. like this:

试试吧像这样:

RewriteRule (index\.php)(?n=)(.*) /pages/$3 [r=301]

#1


You probably want to catch "index.php.*". Otherwise mod_rewrite only replaces the "index.php" part of the URL "index.php?n=text" with the new URL.

你可能想要捕获“index.php。*”。否则mod_rewrite只用新URL替换URL“index.php?n = text”的“index.php”部分。

#2


Guss,

From what you suggested, i reconstructed it as follows:

根据你的建议,我重建如下:

RewriteCond %{QUERY_STRING} ^n=(.*)$
RewriteRule index.php.* http://www.sitename.com/pages/%1 [r=301,nc]

This doesnt seem to be working either. Can you please elaborate on what you have said?

这似乎也没有工作。你能详细说明你所说的话吗?

thank you aditya

谢谢aditya

#3


don´t use the url in the rewrite rule, apache then sends a http 200 code and then the 301...

不要在重写规则中使用url,apache然后发送一个http 200代码然后发送301 ...

try sth. like this:

试试吧像这样:

RewriteRule (index\.php)(?n=)(.*) /pages/$3 [r=301]