使用htaccess忽略链接的某些部分

时间:2021-08-06 11:18:38

I have a question about htaccess and it's rewritings.
I have this code:

我有一个关于htaccess的问题,它是重写。我有这个代码:

Options +FollowSymLinks  
RewriteEngine On  

RewriteCond %{SCRIPT_FILENAME} !-d  
RewriteCond %{SCRIPT_FILENAME} !-f  

RewriteRule ^users/(\d+)*$ ./profile.php?id=$1  
RewriteRule ^threads/(\d+)*$ ./thread.php?id=$1  

RewriteRule ^search/(.*)$ ./search.php?query=$1  

which example.com/users/123 is equals to example.com/profile.php?id=123.

其中example.com/users/123等于example.com/profile.php?id=123。

if I change link to this: example.com/users/123/John
Will htaccess ignore /John or any extra characters after the ID?
the fact, John is the real-name of 123 ID, I want it to be.

如果我更改链接到这个:example.com/users/123/John请问htaccess忽略/ John或ID之后的任何额外字符?事实上,约翰是123 ID的真实姓名,我希望它是。

2 个解决方案

#1


4  

No it won't ignore extra parts from your URL since you're using $ (line end) in the regex here:

不,它不会忽略您的URL中的额外部分,因为您在此处使用正则表达式中的$(行尾):

^users/(\d+)*$ 

Change your rules to:

将您的规则更改为:

RewriteCond %{SCRIPT_FILENAME} !-d [OR]
RewriteCond %{SCRIPT_FILENAME} !-f  
RewriteRule ^ - [L]

RewriteRule ^users/(\d+) profile.php?id=$1 [L]

RewriteRule ^threads/(\d+) thread.php?id=$1 [L]

RewriteRule ^search/(.*)$ search.php?query=$1 [L]

#2


2  

When I was doing this kind of search friendly readable links, I took the name part in consideration too, it may be important for certain occasions.

当我做这种搜索友好的可读链接时,我也考虑了名称部分,这对某些场合可能很重要。

If you just ignore everything after the id, then:

如果你只是忽略id之后的所有内容,那么:

http://example.com/users/123/John

and

http://example.com/users/123/Jane

would both point to the same user, while the links are clearly different. It is also possible, that John changes his name later to Andrew, but the link with John in it would still point to him. This is some undesired inconsistency in my mind.

会指向同一个用户,而链接明显不同。也有可能,约翰后来改名为安德鲁,但与约翰的联系仍然指向他。这在我看来是一些不希望的不一致。

My solution was something like this:

我的解决方案是这样的:

RewriteRule ^users/(\d+)(/(.*))?$ profile.php?id=$1&name=$3 [L]

In your code, you can now check if the user with id in $_GET['id'] has the name in $_GET['name'] and if it doesn't, you may redirect to the proper link with 301 Temporarily Moved. This way a wrong link may not end up in search indexes, and your users would always see the proper profile urls. Examples:

在您的代码中,您现在可以检查ID为$ _GET ['id']的用户是否在$ _GET ['name']中具有名称,如果没有,您可以重定向到301临时移动的正确链接。这样,错误的链接可能不会在搜索索引中结束,并且您的用户将始终看到正确的个人资料网址。例子:

http://example.com/users/123/John -> nothing happens
http://example.com/users/123      -> redirect to /123/John
http://example.com/users/123/Jane -> redirect to /123/John
http://example.com/users/123Jane  -> not found, bad link format

#1


4  

No it won't ignore extra parts from your URL since you're using $ (line end) in the regex here:

不,它不会忽略您的URL中的额外部分,因为您在此处使用正则表达式中的$(行尾):

^users/(\d+)*$ 

Change your rules to:

将您的规则更改为:

RewriteCond %{SCRIPT_FILENAME} !-d [OR]
RewriteCond %{SCRIPT_FILENAME} !-f  
RewriteRule ^ - [L]

RewriteRule ^users/(\d+) profile.php?id=$1 [L]

RewriteRule ^threads/(\d+) thread.php?id=$1 [L]

RewriteRule ^search/(.*)$ search.php?query=$1 [L]

#2


2  

When I was doing this kind of search friendly readable links, I took the name part in consideration too, it may be important for certain occasions.

当我做这种搜索友好的可读链接时,我也考虑了名称部分,这对某些场合可能很重要。

If you just ignore everything after the id, then:

如果你只是忽略id之后的所有内容,那么:

http://example.com/users/123/John

and

http://example.com/users/123/Jane

would both point to the same user, while the links are clearly different. It is also possible, that John changes his name later to Andrew, but the link with John in it would still point to him. This is some undesired inconsistency in my mind.

会指向同一个用户,而链接明显不同。也有可能,约翰后来改名为安德鲁,但与约翰的联系仍然指向他。这在我看来是一些不希望的不一致。

My solution was something like this:

我的解决方案是这样的:

RewriteRule ^users/(\d+)(/(.*))?$ profile.php?id=$1&name=$3 [L]

In your code, you can now check if the user with id in $_GET['id'] has the name in $_GET['name'] and if it doesn't, you may redirect to the proper link with 301 Temporarily Moved. This way a wrong link may not end up in search indexes, and your users would always see the proper profile urls. Examples:

在您的代码中,您现在可以检查ID为$ _GET ['id']的用户是否在$ _GET ['name']中具有名称,如果没有,您可以重定向到301临时移动的正确链接。这样,错误的链接可能不会在搜索索引中结束,并且您的用户将始终看到正确的个人资料网址。例子:

http://example.com/users/123/John -> nothing happens
http://example.com/users/123      -> redirect to /123/John
http://example.com/users/123/Jane -> redirect to /123/John
http://example.com/users/123Jane  -> not found, bad link format