Htaccess and user accounts [duplicate]

2020-05-07 10:11发布

You know how twitter writes its urls like this: twitter.com/{username}?

How do they achieve that? There must be some htaccess coding involved in it. I tried it but my code gets confused when its a username or a something else and shows the same page again and again.

Here is my code:

RewriteRule ^([^/]+)$ login/id.php?name=$1
RewriteRule ^#/([^/]+)$ tags/tag.php?name=$1

So if I write www.mysite.com, it takes me to the home page, and if I type www.mysite.com/{random name that is not a register user}, it again takes me to the home. This creates a major error in the url. www.mysite.com/index and www.mysite.com/index2 go to the same page even though they are two different files. By the way, I have set in the htaccess to hide file extensions.

Please help me

2条回答
趁早两清
2楼-- · 2020-05-07 10:29
RewriteRule ^(.*)?$ tags/tags.php?name=$1 [NC,L]

This alone will rewrite everything to the tags/tags.php file. You could use conditionals to exclude certain important paths.

RewriteCond %{REQUEST_URI} !^\/?accounts[.*]$ [NC]
RewriteRule ^(.*)?$ tags/tags.php?name=$1 [NC,L]

... i think the rewrite condition syntax is right but I haven't tested it ...

So everything in accounts/ is granted as expected and everything else routes to the tags file. Depending on how complex your site is, this could be a good or bad idea. If your focus is on users and you have few other pages (like about, faq, contact, etc) then you should be fine. Otherwise you may be getting into a nightmare for conditional sorting.

I used tags.php as an example as well as /accounts. I don't know the structure of your site so I don't know your exact needs. This should be enough to get you started.

My point is that you might have to be ready to have a lot of conditionals above your tags.php htaccess rule since you are comparing against the first uri segment. If your hopeful path was more like www.yoursite.com/user/username it would be a lot easier.

查看更多
来,给爷笑一个
3楼-- · 2020-05-07 10:42

htaccess is not best for performance and is more limited than handling the routing with php. First, you use htaccess only to direct most requests to a php file:

RewriteCond %{REQUEST_URI} !\.(png|jpe?g|gif|css|js|html)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php [L]

Then, your routing could be as simple as:

$pieces = preg_split('-/-', $_SERVER['REQUEST_URI'], NULL, PREG_SPLIT_NO_EMPTY);
$username = $pieces[0];

include "users/{$username}.php";

but there is a lot more to do in a real application context. There are many php frameworks that have routing features built in that you may be interested in. Any major framework will include this.

Here's a slighly expanded version of this answer on a similar previous post: https://stackoverflow.com/a/20034826/1435655

To expand a bit more, the basic idea most routing systems use is you write out the routes that you accept and what will handle them. When a request comes in, those routes are searched until a match is found. Think of it like this:

$myRoutes = [
  '/' => 'home.php',
  '/about' => 'about.php'
  //etc
];

$route = $_SERVER['REQUEST_URI'];
if (in_array($route, $myRoutes)) {
  include $myRoutes[$route];
}
else {
  echo "Go to a real page. This one doesn't exist."
}

You could expand that to define url parameters so that /users/someDude will be sent to users.php and something like $routeParams['username'] will have a value of someDude.

查看更多
登录 后发表回答