简体   繁体   English

.htaccess mod_rewrite-保留的文件夹名称

[英].htaccess mod_rewrite - reserved folder names

I'm rewriting a url to a users profile with mod_rewrite like this: domain.com/users/JohnSmith. 我正在使用mod_rewrite将网址重写为用户个人资料,如下所示:domain.com/users/JohnSmith。

I was thinking maybe it would be neater to skip the users folder and simply put it like: domain.com/JohnSmith. 我当时在想,跳过users文件夹并简单地将其命名为:domain.com/JohnSmith可能更整洁。

My question: How do I do this and keep my system and site folders? 我的问题:如何执行此操作并保留系统和站点文件夹? If I look for domain.com/browsersupport, the profile.php page will check the name and look for it in a database, and since no user has that name, no page will be shown. 如果我寻找domain.com/browsersupport,则profile.php页面将检查名称并在数据库中寻找它,并且由于没有用户使用该名称,因此不会显示任何页面。

I know it can be fixed with some if statements in the PHP-code. 我知道可以用PHP代码中的某些if语句解决此问题。 Also, the folders name need to be reserved so users can't register those names. 另外,文件夹名称需要保留,以便用户无法注册这些名称。 But for the profile look up - is there a neater way? 但是要查找个人资料-有没有更整洁的方法? Maybe to exclude the system folders in the .htaccess file so profile.php won't go look for that user? 也许排除.htaccess文件中的系统文件夹,以便profile.php不会寻找该用户?

That is where these 2 conditions come into play: 这就是这两个条件起作用的地方:

  # if the request does NOT map to a file
  RewriteCOnd %{REQUEST_FILENAME} !-f
  # and the request does NOT map to a directory
  RewriteCOnd %{REQUEST_FILENAME} !-d
  # only THEN use this rewriterule
  RewriteRulle ....etc...

If you have both 'dynamic' pages, and dynamic users, the RewriteRule should end in a process that checks whether a page by that name exists, and only if it doesn't, serve the user page. 如果你同时拥有“动态”页面和动态用户,重写规则应以检查由该名称的页面是否存在一个过程结束,且仅当它不,为用户服务页面。

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule #insert user rewrite rule here

This will only apply the rewrite rule if a folder or file by that given name does not exist (your question seems to imply that browsersupport and the like do exist in the document root like that). 仅在不存在具有该给定名称的文件夹或文件的情况下,这才适用重写规则(您的问题似乎暗示那样, browsersupport等确实存在于文档根目录中)。

Yes you will have to be diligent about letting users know that they can't register certain names like that. 是的,您必须努力让用户知道他们无法注册某些特定名称。 If they do, they won't be able to access their username by that route. 如果这样做,他们将无法通过该路由访问其用户名。

I've done this, but I have a self-imposed rule that is probably not for everyone but works for me: usernames must be 5 characters at least, and controllers are 4 or less. 我已经做到了,但是我有一个自我强加的规则,该规则可能不适合所有人,但对我有用:用户名至少必须为5个字符,控制器为4个或更少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM