.htaccess 用查询字符串重写 URL [英] .htaccess Rewrite URL with a query string

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

问题描述

我尝试了在 stackoverflow 上发布的大量解决方案,但似乎对我没有任何作用.我想在我的网站上重写一些网址,以便对搜索引擎友好.

I have tried a ton of solutions posted at stackoverflow but nothing seems to work for me. I would like to rewrite a few URLs on my site to be search engine friendly.

我想要这样的网址:-http://www.mysite.com/index.php?filter=accounts显示为:-http://www.mysite.com/accounts

I would like a URL like this:- http://www.mysite.com/index.php?filter=accounts To display as:- http://www.mysite.com/accounts

网络上的很多帖子都给出了解决方案:-

A lot of posts around the web give this as the solution:-

RewriteRule ^([^/]+)/?$ index.php?filter=$1 [QSA,L]

但这没有任何作用.

我的站点是一个 Joomla CMS 站点,并且在 .htaccess 文件中已经有一些重写.会不会是我把新的 RewriteRule 放错了地方?

My site is a Joomla CMS site and there are already some rewrites within the .htaccess file. Could it be that I am putting my new RewriteRule is the wrong place?

这是没有添加我的新 RewriteRule 的完整 .htaccess 文件

Here is the full .htaccess file without my new RewriteRule added

Options +FollowSymLinks
RewriteEngine On

# prevents people from accessing anything with phpMyAdmin
RewriteRule phpMyAdmin - [F]

# force canonical www if request is for non-www or has port number etc
RewriteCond %{HTTP_HOST} !^(www.mysite.com)?$
RewriteRule (.*) http://www.mysite.com/$1 [R=301,L]

RewriteCond %{QUERY_STRING} mosConfig_[a-zA-Z_]{1,21}(=|\%3D) [OR]
RewriteCond %{QUERY_STRING} base64_encode[^(]*([^)]*) [OR]
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|[|\%[0-9A-Z]{0,2})
RewriteRule .* index.php [F]
RewriteBase /
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} (/[^.]*|.(php|html?|feed|pdf|raw))$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]

任何帮助将不胜感激.:-)

Any help would be much appreciated. :-)

推荐答案

UPDATED

现在测试了.试试这个:

Now is tested. Try this:

RewriteEngine On
RewriteRule ^([a-zA-Z0-9-=_?]+)/?$ index.php?filter=$1 [L] 

.htaccess 进入根目录.

请求的 URL 是 http://www.mysite.com/accounts/ 但可以是任何内容:http://www.mysite.com/apples/.

The requested URL is http://www.mysite.com/accounts/ but could be anything: http://www.mysite.com/apples/.

注意:地址栏中显示的 URL 是在该地址栏中输入的 URL.我假设不是 http://www.mysite.com/index.php?filter=accounts 因为我猜重定向的目的是用更友好的(http://www.mysite.com/accounts),这是必须在地址栏中输入的,而不是相反.有点令人困惑,但我希望它是有道理的.

NOTE: The URL displayed in the address bar is the one that was entered in that address bar. I assume is not http://www.mysite.com/index.php?filter=accounts because I guess the purpose of the redirection is to replace it with a more friendly one (http://www.mysite.com/accounts), which is the one that has to be typed in the address bar, not the other way around. Little confusing but I hope it makes sense.

要测试它,请在根目录中的 index.php 中包含以下唯一代码.

To test it, include the following only code at index.php in root directory.

<?php 

if ($_GET['filter'] == 'accounts') {
echo "This is Accounts<br /><br />";    
}else {
echo "This is INDEX<br /><br />";       
}

?>

至于在.htaccess 中的何处放置重写规则,我不知道.我想你必须在实际规则之前和之后尝试一下.

As to where to place the rewrite rules inside .htaccess, I could not know. I guess you have to try it before and after the actual rules.

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

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