Apache mod rewrite like twitter url


Apache mod rewrite like twitter url

这是我的.htaccess代码

  ErrorDocument 404 /404.php
  Options -Multiviews
  RewriteEngine On
 RewriteBase /
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteCond %{REQUEST_FILENAME} !-d
 RewriteRule ^([^/]+)/?$ profile?username=$1 [NC,L]
 RewriteCond %{REQUEST_FILENAME}!-f
 RewriteCond %{REQUEST_FILENAME}!-d
 RewriteRule ^([a-z]+)/([a-z'-]+)$ /$1/$2.php 
 RewriteRule ^s/([^/]*)$ /s.php?share=$1 [L]
 RewriteCond %{HTTP_HOST} !^$
 RewriteCond %{HTTP_HOST} ^mywebsite.com [NC]
 RewriteCond %{HTTPS}s ^on(s)|
 RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

我希望我网站上的用户能够像Twitter一样访问他们的个人资料。例如,www.mywebsite.com/Jim 实际上会被重写为 www.mywebsite.com/profile?username=Jim。

目前,代码中唯一有效的部分是允许没有 php 扩展名的 url。例如,www.mywebsite.com/home 而不是 www.mywebsite.com/home.php。

此外,s.php 的重写规则无法正常工作。我希望页面 www.mywebsite.com/s/123456 写成 www.mywebsite.com/s?share=12345。

如果我的 htaccess 顺序错误,我不知道,但这些似乎都不起作用。此外,当找不到页面时,即使我有重写规则为 404,我仍然会获得默认的 Apache 404.php

您的重定向需要首先是您的个人资料,然后是您的共享,然后是最通用的 php 扩展:

ErrorDocument 404 /404.php
Options -Multiviews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} ^mywebsite.com [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ profile.php?username=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^s/([^/]*)$ /s.php?share=$1 [L]
RewriteCond %{REQUEST_FILENAME}!-f
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^([a-z]+)/([a-z'-]+)$ /$1/$2.php [L]