0

我正在尝试在我的应用程序中建立良好的链接。

我决定重写如下所示的链接:

1. http://example.cz/get1/get2/get3
2. http://example.cz

进入这些(我只有php应用程序):

1. http://example.cz/index.php?path=get1+get2+get3
2. http://example.cz/index.php?path=

我在链接之前删除 www。

我一直无法将其重写为.htaccess.

如果将 get params 重写为 path=get1+get2+get3 的主要想法是好的,我也在寻找建议?现在我可以看到这样的链接http://www.example.cz/you+me/可能会在某个地方失败。你有更好的解决方案吗?

所以问题是:如何将其重写为 .htaccess 以及如何解决包含“+”的链接可能出现的问题

编辑:

我稍微提高了我的技能,我做到了:

RewriteEngine on
Options +FollowSymlinks

RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule (.*) http://%1/$1 [R=301,L]

RewriteCond %{REQUEST_URI} !^\/index.php(.*)
RewriteRule ^(.+) /index.php?path=/$1 [R=301,L] # 301 is here so I can see how does it work

# everything above works well (as I want)
# link now look like this one: 
# http://example.net/index.php?path=/get1/get2/get3
# Now I was looking for universal rule that will rewrite my get params...

# First I did this:
RewriteCond %{REQUEST_URI} /([^/]+) 
RewriteCond %1 !index.php(.*)
RewriteRule /([^/]+) $1+ [R=301,L]

# If any part of request uri is string that matches pattern /([^/]+) 
# And if ([^/]+) doesn't match index.php(.*)
# then rewrite every /([^/]+) into $1+
# Now I see that it is incorrect, but I wasn't able to fix it

# So then I did different rule
RewriteRule ^([^=]+=[^\/]*)\/([^\/]+)(.*)$ $1$2+$3 [R=301,L]

# start of string
# first var is: one or more chars except =, =, zero or more chars except /
# /
# second var is: one or more chars except /
# third var is: zero or more chars
# end of string

我认为第二个想法要好得多,但它也不起作用。请帮我修复它。

4

2 回答 2

1

您可以使用 Apache 模块来做到这一点mod_rewrite。您可能已经安装了它。尝试这个:

RewriteEngine On
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ index.php?path=$1+$2+$3 [L]

此正则表达式假定 URL 将始终在斜杠之间包含三组文本。您可以根据需要对其进行调整。

另请注意,Apache 永远不会看到 URL 哈希,因此您将无法在重写规则中匹配它。幸运的是,看起来你无论如何都不想用它做任何事情。只需使用上面的规则,哈希将保留在浏览器中 URL 的末尾。

于 2013-07-03T14:49:31.520 回答
0

我做了解决方案。问题是在添加 index.php?path= 之后我无法使用查询字符串......

http://www.example.net/get1/get2/get3将链接从变为 的最终通用解决方案http://example.net/index.php?path=get1+get2+get3

RewriteEngine on
RewriteBase /
Options +FollowSymlinks

RewriteCond %{HTTP_HOST} ^www\.(.+)$
RewriteRule (.*) http://%1/$1 [R=301,L]

RewriteCond %{REQUEST_URI} !^\/index.php(.*)
RewriteRule ^(.+) /index.phppath=/$1 [R=301,L]

RewriteRule ^([^=]+=[^/]*)/([^/]+)(.*)$ $1$2+/$3 [R=301,L]
RewriteRule ^(.*)\+/+$ $1 [R=301,L]
RewriteRule ^(.*)path=(.*)$ $1?path=$2 [R=301,L]
于 2013-07-04T11:36:58.383 回答