.htaccess 到不同的文件取决于输入 URL

.htaccess To Different Files Depending On Inputed URL

我正在尝试制作个人资料页面和编辑个人资料页面。配置文件页面运行良好,URL 存储在数据库中。它是这样工作的:

  1. 用户输入 URL (localhost/postin'/profiles/username)
  2. 输入 URL 后,.htaccess 文件重定向到 profile.php 文件,其中显示配置文件信息

上面的效果很好,.htaccess 文件如下所示:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w.'@\\/-]+)$ profile.php?user= [NC,L]

<Files .htaccess>
order allow,deny
deny from all
</Files>

当我 localhost/postin'/profiles/edit/username 时会发生什么?它不适用于我当前的 .htaccess,因为它认为编辑是用户名的一部分,而实际上它只是另一个文件。我试过 .htaccess:

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w.'@\\/-]+)$ profile.php?user= [NC,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^edit/([\w.'@\\/-]+)$ /profile.php?user= [NC,L]

<Files .htaccess>
order allow,deny
deny from all
</Files>

但似乎没有用! :(

下面是一个简单的例子,说明我希望它做什么。用户的用户名是 hawkeye。因此,“hawkeye”输入 localhost/postin'/profiles/hawkeye 以访问他的个人资料。然后为了编辑他的个人资料,他输入 localhost/postin'/profiles/edit/hawkeye.

这个 .htaccess 文件是什么样子的?谢谢! :)

更新

这是我正在使用的 .htaccess:

Options +FollowSymLinks
RewriteEngine on

# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^edit/([\w.'@\\/-]+)$ editprofile.php?user= [NC,L]
RewriteRule ^([\w.'@\\/-]+)$ profile.php?user= [NC,L]

<Files .htaccess>
order allow,deny
deny from all
</Files>

当我尝试 URLS localhost/postin'/profiles/usernamelocalhost/postin'/profiles/edit/username 它总是去 localhost/postin'/profiles/username 无论如何......有什么想法吗? :D 谢谢

固定

Options +FollowSymLinks
RewriteEngine on

# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^edit/([\w.'@\\/-]+)$ editprofile.php?user= [NC,L]
RewriteRule ^([\w.'@\\/-]+)$ profile.php?user= [NC,L]

您可以在以下地方使用它:localhost/postin'/profiles/.htaccess :

Options +FollowSymLinks
RewriteEngine on

# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^edit/([\w.'@\\/-]+)$ editprofile.php?user= [NC,L]
RewriteRule ^([\w.'@\\/-]+)$ profile.php?user= [NC,L]

重定向 profiles/username -> profiles/profile.php?user=username
重定向 profiles/edit/username -> profiles/editprofile.php?user=username