【问题标题】:Rewrite rule for clean url为干净的 url 重写规则
【发布时间】:2014-10-05 02:03:43
【问题描述】:

我正在尝试为我的网站上的个人资料编写一个干净的网址。例如我想要这个网址

/profile?user=MrEasyBB

成为

/MrEasyBB

我现在的htaccess 是这样开始的

Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ profile.php?user=$1 [NC,L]

DirectoryIndex index.php

如果我是正确的,这一切都应该有效。但是如果他们输入的页面网址是

/help

不应有名为help 的用户,而该页面应指向help.php。我可以写一条不遵循某种模式的规则吗?我也想知道更改图像、cmets 等的规则:

IE

/image/user/MrEasyBB/1239123871712194124.jpg

而不是

/image/picture.php?user=MrEasyBB&image=1239123871712194124.jpg

如果第一个不起作用以及如何执行第二个,我对如何添加倍数和遵循模式不肯定需要任何关于重写规则的建议?

我还下载了 htaccess 备忘单 pdf 以了解更多信息

【问题讨论】:

  • http://domain.com/image/user/MrEasyBB/1239123871712194124.jpg 图片的完整路径是否正确?
  • 是的,它会是。会有.../image/user/....../video/user/... 我想遵循的模式
  • 如果http://domain.com/image/user/MrEasyBB/1239123871712194124.jpg 确实是图像的正确路径,那么您的规则将不会影响此图像,因为RewriteCond %{REQUEST_FILENAME} !-f 条件意味着不对有效文件执行规则。
  • 不,这不是整个图像的正确路径。这是我想要遵循的正确模式。正确的路径将位于一般图像文件夹中。我只是不允许直接访问它。每说他们去domain.com/image/1239....jpg 它不会让他们访问这个。我希望这是可以理解的。我只需要 MrEasyBB 部分就可以知道 PHP 中喜欢谁的图像。虽然图像位于所有图像的部分中。
  • 抱歉,目前还不清楚。请指定您要向客户显示的图像路径以及在内部可以找到这些图像的真实路径是什么?

标签: php apache .htaccess mod-rewrite url-rewriting


【解决方案1】:

你需要先有一个规则来重写图像以正确的路径,然后你的catch all规则就会出现:

DirectoryIndex index.php
Options -MultiViews
RewriteEngine On
RewriteBase /

RewriteRule ^(image)/[^/]+/(.+)$ /$1/$2 [NC,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^user/(.+)$ profile.php?user=$1 [NC,L,QSA]

【讨论】:

  • 谢谢,我试试看
  • 这不会解决 /help 的情况,但如果您不介意 /help.php,那么它将被视为现有文件,并提供该文件。基本上,您会希望按 URL 的类型/目的对 URL 进行分组,因此在这种情况下,我建议 RewriteRule ^/user/(.+)$ profile.php?user=$1 以便 /user/MrEasyBB 转到 profile.php?user=MrEasyBB。这为您提供了制定其他规则以允许不同页面的空间,例如 /help。
  • 毫无疑问,拥有/user/MrEasyBB URL 会更好、更安全。我只是保持该规则与您的问题相同,因为我不知道您是否会考虑使用/user/MrEasyBB URL 而不是/MrEasyBB。现在对其进行了相应的编辑。
【解决方案2】:

类似:

Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?image/user/([a-zA-Z0-9_-]+)/([a-zA-Z0-9.]+)$ /image/picture.php?user=$1&image=$2 [NC,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ profile.php?user=$1 [NC,L]

DirectoryIndex index.php

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-02-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-24
    • 2023-03-30
    • 2012-12-24
    相关资源
    最近更新 更多