RewriteRule - 检查URL中是否存在某些Term,如果不存在则

时间:2021-06-23 10:29:06

I am currenctly facing some htaccess/rewriterule issues. (And I am new to this area)

我当前面临一些htaccess / rewriterule问题。 (我是这个领域的新手)

Let's assume we have an url like this: http://mypage.at/very/cool

我们假设我们有一个这样的网址:http://mypage.at/very/cool

The URL is supposed to look like this (Cause I am using an AJAX-loadedContent which requires this): http://mypage.at/#ajx/very/cool

URL应该看起来像这样(因为我正在使用一个需要这个的AJAX-loadedContent):http://mypage.at/#ajx/very/cool

So I would like to add the part '#ajx' to every url which does not already contain it. Which means if an url does already look like: http://mypage.at/#ajx/so/pretty then there is no need for changes.

所以我想将“#ajx”部分添加到每个尚未包含它的网址中。这意味着如果网址看起来像:http://mypage.at/#ajx/so/pretty那么就没有必要进行更改。

As I am not sure wheter this creates troubles with the GoogleSearchIndex, I would additionally like to know if there is a way to exclude this rule for searchbots.

由于我不确定这会给GoogleSearchIndex造成麻烦,我还想知道是否有办法排除搜索机器人的这条规则。

Thanks for any help. Ripei

谢谢你的帮助。 Ripei

3 个解决方案

#1


1  

Since you reported that this does not work (which is probably because your version of Apache doesn't support Perl-style RegEx):

由于您报告这不起作用(这可能是因为您的Apache版本不支持Perl样式的RegEx):

RewriteRule ^(?!#ajx)(.*)$ http://mypage.at/#ajx/$1 [L]

I think this should do it:

我认为应该这样做:

RewriteCond %{REQUEST_URI} !^/#ajx
RewriteRule ^(.*)$ http://mypage.at/#ajx/$1 [L]

EDIT: After trying this myself and reading around on the Internet, I'm not sure this is actually possible. A pound sign (#) is not a legal part of a URL. This answer comes close, but I'm going to have to leave this to somebody who knows more to say whether this can even be done the way @Ripei asked for.

编辑:自己尝试并在互联网上阅读后,我不确定这是否可行。井号(#)不是URL的合法部分。这个答案很接近,但是我不得不把这个留给那些知道更多的人来说是否可以按照@Ripei的要求来做。

#2


0  

Something like this might work:

这样的事情可能有用:

RewriteCond %{REQUEST_URI} !^/#ajx
RewriteRule ^(.*) http://%{SERVER_NAME}/#ajx$1 [R,L]

#3


0  

Hah finally after lot's of reading and testing - I got it.

哈哈终于经过多次阅读和测试 - 我明白了。

RewriteCond %{REQUEST_URI} !^#ajx
RewriteRule . /\#ajx%{REQUEST_URI} [L,R,NE]

This Code works for me... don' ask me why this one is working, to be honest I do not have a clue! But well I am fine with the fact, that it IS working :)

本规范对我有用...不要问我为什么这个有用,说实话我没有线索!但我很好,因为它工作:)

#1


1  

Since you reported that this does not work (which is probably because your version of Apache doesn't support Perl-style RegEx):

由于您报告这不起作用(这可能是因为您的Apache版本不支持Perl样式的RegEx):

RewriteRule ^(?!#ajx)(.*)$ http://mypage.at/#ajx/$1 [L]

I think this should do it:

我认为应该这样做:

RewriteCond %{REQUEST_URI} !^/#ajx
RewriteRule ^(.*)$ http://mypage.at/#ajx/$1 [L]

EDIT: After trying this myself and reading around on the Internet, I'm not sure this is actually possible. A pound sign (#) is not a legal part of a URL. This answer comes close, but I'm going to have to leave this to somebody who knows more to say whether this can even be done the way @Ripei asked for.

编辑:自己尝试并在互联网上阅读后,我不确定这是否可行。井号(#)不是URL的合法部分。这个答案很接近,但是我不得不把这个留给那些知道更多的人来说是否可以按照@Ripei的要求来做。

#2


0  

Something like this might work:

这样的事情可能有用:

RewriteCond %{REQUEST_URI} !^/#ajx
RewriteRule ^(.*) http://%{SERVER_NAME}/#ajx$1 [R,L]

#3


0  

Hah finally after lot's of reading and testing - I got it.

哈哈终于经过多次阅读和测试 - 我明白了。

RewriteCond %{REQUEST_URI} !^#ajx
RewriteRule . /\#ajx%{REQUEST_URI} [L,R,NE]

This Code works for me... don' ask me why this one is working, to be honest I do not have a clue! But well I am fine with the fact, that it IS working :)

本规范对我有用...不要问我为什么这个有用,说实话我没有线索!但我很好,因为它工作:)