我做了几次尝试,但都没有成功。这就是我想要做的“静态动态url":
index.php?content=authors => /compositori/
index.php?content=albums => /dischi/查询字符串变量值"authors“需要变为"compositori",我尝试过这样做,但没有结果:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^compositori/$ index.php?content=authors - [R=301,L]
</IfModule>这是针对动态urls的:
index.php?content=author&c=nome-compositore => /nome-compositore/
index.php?content=author&c=nome-compositore&action=view-bio => /nome-compositore/biografia/
index.php?content=author&c=nome-compositore&action=view-albums => /nome-compositore/dischi/
index.php?content=author&c=nome-compositore&action=view-album&a=nome-disco => /nome-compositore/nome-disco/然后,我想将这个url " index.php?content=index“重定向到主页,不带任何查询字符串参数,在.com,index.php?content=index => www.mydomain.com之后也不带任何内容
对于所有这些规则,我还需要使它们在子文件夹上工作,htaccess位于该子文件夹中,例如mydomain.com/test/。已尝试使用RewriteBase,但没有结果:/
提前感谢
编辑:多亏了David,我现在有了这些规则:
RewriteRule ^/?$ /subfolder/index.php?content=index [L]
RewriteRule ^contatti/$ /subfolder/index.php?content=contacts [L]
RewriteRule ^compositori/$ /subfolder/index.php?content=authors [L]
RewriteRule ^dischi/$ /subfolder/index.php?content=albums [L]
RewriteRule ^(.+)/biografia/$ /subfolder/index.php?content=author&c=$1&action=view-bio [L]
RewriteRule ^(.+)/dischi/$ /subfolder/index.php?content=author&c=$1&action=view-albums [L]
RewriteRule ^(.+)/(.+)/$ /subfolder/index.php?content=author&c=$1&action=view-album&a=$2 [L]但我注意到,如果我将未重写的查询字符串(index.php?content=authors)放入url,它会加载内容,但仍保留未格式化的字符串,并且不会重定向到重写的url。我试着把R=301,L放进去,但什么也没做。这是我的最后一期。
再次感谢
发布于 2013-02-03 08:18:09
RewriteRule ^compositori/$ index.php?content=authors - [R=301,L]
这是无效的,原因有两个:您有三个规则区域,而不是所需的两个区域(末尾有额外的- ),并且缺少一个前导斜杠。您还将其设置为重定向(R=301),但可能只是想重写。应将其更改为:
RewriteRule ^compositori/$ /index.php?content=authors [L]
一旦你得到了工作,你发布的其余转换应该是类似的工作。
然后我想将这个url " index.php?content=index“重定向到主页,不带任何查询字符串参数,在.com,index.php?content=index => www.mydomain.com之后也不带任何内容
您使用的是术语“重定向”,但根据前面示例中箭头的方向,您可能表示相反方向的“重写”,所以这是两种方式的答案:
要让用户输入www.mydomain.com并让服务器实际加载index.php?content=index (我认为这是您真正的意思):
RewriteRule ^/?$ /index.php?content=index [L]
要真正将index.php?content=index重定向到www.mydomain.com,请执行以下操作
RewriteCond %{QUERY_STRING} ^content=index$
RewriteRule ^index.php$ http://www.mydomain.com/ [R=301,L]编辑:关于您的最后一个问题,关于将带有查询字符串的完整路径重定向到“漂亮”的URL,您将实现我在上面为每个特定重写编写的内容(RewriteCond + RewriteRule),并将这些规则放在现有(新)规则之上。换句话说:
RewriteCond %{QUERY_STRING} ^content=index$
RewriteRule ^subfolder/index.php$ http://www.mydomain.com/ [R=301,L]
RewriteCond %{QUERY_STRING} ^content=contacts$
RewriteRule ^subfolder/index.php$ http://www.mydomain.com/contatti/ [R=301,L]
... repeat for remaining rules
RewriteRule ^/?$ /subfolder/index.php?content=index [L]
RewriteRule ^contatti/?$ /subfolder/index.php?content=contacts [L]
... repeat for remaining rules这导致每个带有查询字符串的完整URL被重定向到pretty URL,然后pretty URL被静默重写为完整URL。关于上面列出的新规则,有几个注意事项:
首先,您向原始问题中没有的每个规则添加了一个/subfolder。以防您希望规则应用于包含或不包含子文件夹的情况下,您可以将该部分添加到括号中,并在末尾添加问号,如下所示:
RewriteRule ^(subfolder/)?index.php$ http://www.mydomain.com/ [R=301,L]此外,您编写的新规则要求匹配路径以斜杠结尾。由于以斜杠结尾或不以斜杠结尾的目录路径是等效的,因此您应该像我在上面的示例(contatti/?)中那样,通过在斜杠后添加问号来使其成为可选的:
https://stackoverflow.com/questions/14665783
复制相似问题