.htaccess :日期范围帮助

发布于 2024-11-18 19:26:23 字数 387 浏览 0 评论 0原文

嗨,我的网址是这样的 http://www.mysite.come/events.php?eid=10

活动开始日期是 01-07-2011

活动名称:夏日之旅

我想将上面的 URL 显示为类似 http://www.mysite.com/events/01/07/2011 /summer-trip

这有意义吗?

我将感谢您的所有回答,请尽快提供帮助。

谢谢

Hi my URL is something like
http://www.mysite.come/events.php?eid=10

Event starting date is 01-07-2011

Event name : summer trip

I want to display the above URL to be like
http://www.mysite.com/events/01/07/2011/summer-trip

Does this make sens!?

I will appreciate all your answers, please help as soon as possible.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你如我软肋 2024-11-25 19:26:23

尝试如下操作:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]

这是一个包罗万象的重定向 - 如果不存在与请求匹配的文件,则该请求将使用原始 url 传递到 index.php。 (大多数框架、wordpress 等都是这样做的)

然后你将在 index.php 中获得 $_GET['url'] ,你可以在 php 中解析它并执行任何需要执行的操作来检查/加载相关内容。

请注意,您需要确保在 PHP 中实现 404 错误 - 因为 apache 永远不会再出现 404 错误。

Try something like:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?url=$1 [QSA,L]

It's a catch all redirect - if a file doesn't exist to match a request, the request is passed to index.php with the original url. (it's how most of the frameworks, wordpress etc do it)

Then you will have $_GET['url'] available in index.php, which you can parse in php and do whatever needs doing to check for/load relevant content.

Note that you need to ensure you implement a 404 error in PHP here - as apache will never 404 itself any more.

烙印 2024-11-25 19:26:23

如果您只想以友好的格式发送日期,这是一个非常简单的重定向:

# Untested example
RewriteEngine On
RewriteRule ^events/(\d{2})/(\d{2})/(\d{4})/.* events.php?date=$3-$2-$1

但是,mod_rewrite 不会发挥作用。无法从 01/01/2011/summer-trip 获取 10


编辑:我想我不应该从字面上理解这个问题并提供一些提示。如果您想通过友好的字符串链接事件,则需要进行一些内部更改:

  1. 向数据库中的事件表添加一个新列并使其唯一(大多数 DBMS 允许创建唯一索引)。该新列(我们称之为 url_title)将保存友好字符串。

  2. 调整您的 PHP 脚本,使其采用 url_title 作为参数,而不是数字键。

你的 mod_rewrite 规则将是这样的:

# Again, untested
RewriteEngine On
RewriteRule ^events/\d{2}/\d{2}/\d{4}/([^/]+)/?$ events.php?url_title=$1 [L]

If you just want to send the date in a friendly format, it's quite a simple redirection:

# Untested example
RewriteEngine On
RewriteRule ^events/(\d{2})/(\d{2})/(\d{4})/.* events.php?date=$3-$2-$1

However, mod_rewrite won't do magic. There's no way to obtain 10 from 01/01/2011/summer-trip.


Edit: I guess I should not take the question literally and provide some hints. If you want to link events by a friendly string, you need to do a couple of internal changes:

  1. Add a new column to the events table in your database and make it unique (most DBMS allow to create unique indexes). That new column (let's call it url_title) will hold the friendly string.

  2. Adjust your PHP script so it takes the url_title as argument, rather than the numeric key.

And your mod_rewrite rule will be something like this:

# Again, untested
RewriteEngine On
RewriteRule ^events/\d{2}/\d{2}/\d{4}/([^/]+)/?$ events.php?url_title=$1 [L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文