php - URL 重写规则在 php 中不起作用

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

我正在使用 godaddy 托管和插件域。我想将长网址改为短网址。

例如我的网址如下: www.example.com/events/landing_event.php?url=text-first

我想要以下网址: www.example.com/events/text-first

为此,我在 .htaccess 文件中使用以下重写网址

RewriteRule ^([a-zA-Z0-9-/]+)$ events/landing_event.php?url=$1 [L]

RewriteRule ^([a-zA-Z0-9-/]+)/$ events/landing_event.php?url=$1 [L]

但是这个重写规则不起作用。 所以请帮助我如何制作短网址。

最佳答案

在确保 RewriteEngine EnabledOn 后使用此源

RewriteEngine On
RewriteRule ^events/([^/]*)$ /events/landing_event.php?url=$1 [L]

原网址: http://www.example.com/events/landing_event.php?url=text-first

重写后的网址: http://www.example.com/events/text-first

更新了 500 内部服务器错误

Your code is guaranteed to generate 500 internal server error because it is causing infinite looping. Reason is that your matching URI pattern is: ^events/([^/]*)$

Which matches your URLs before and after rewrites. And once it reaches max allowed internal rewrite limit Apache throws 500 internal server error and bails out.

将您的代码更改为此

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

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

关于php - URL 重写规则在 php 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38565128/

相关文章:

php - mysql查询检查多个非空列

java - 在将字符串写入 Apache Commons PropertiesConfiguration 之前对其进行转义的正确方法是什么?

WordPress 和 Ubuntu,用于 FTP 的 www-data 和 ubuntu

javascript - CORS 请求在 Chrome 中不起作用

PHP,htaccess : Multiple script executing when use "RewriteBase/" on vhost

php - 如果少于 0(负) 天,则出现 EXPIRED

PHP cURL 不适用于 - 在子域中

php - Symfony 中的文章排序列表

apache - Java 重定向 + 域

apache - 如何获取htaccess中的子域名和域名