php - 漂亮的 URL 条件重写

标签 php apache .htaccess mod-rewrite

我们的网站允许使用漂亮的网址“etest.me/1234”,其中“1234”是客户端 ID。正如您在下面看到的,它重定向到我们进行重定向的route.php 文件。问题是,当客户端使用不带“/1234”的“etest.me”时,他们会得到 apache 的“在此服务器上找不到请求的 URL/go/”。信息。我希望当“/1234”丢失时,该网址能够转到另一个页面。

请注意,我们将域和路径转发到不存在的“/go”目录,因此下面的规则将捕获它。以下内容位于根目录下的 .htaccess 文件中。

RewriteEngine On
#restrict rewriting URLs ONLY to paths that DO NOT exist
RewriteCond %{SCRIPT_FILENAME} !-d
# commented out to speed up looking since we are not processig file names anyway
#RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^go/([a-zA-Z0-9/]+)$ ./route\.php?go=$1

最佳答案

对我来说工作得很好。希望这也对您有用。

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^go/([a-zA-Z0-9]+)$ /route.php?go=$1 //when client id exist
RewriteRule ^([a-zA-Z0-9]+)$ /index.php // when no client id

关于php - 漂亮的 URL 条件重写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43132244/

相关文章:

php - 将行值分配给按钮

php - 连接android与php和mysql报错

php - 语法错误或访问冲突 : 1064 You have an error in your SQL syntax

php - Apache:0777 目录的权限被拒绝

apache - 如何使用 Apache 配置设置 SameSite cookie 属性?

php - php 中的 Server.MapPath 等效项

apache - .htaccess - 如何拒绝除一个以外的所有目录?

wordpress - 如何编写 .htaccess 重写规则

regex - .htaccess 用于 SSL?

.htaccess - 强制 https ://www. 域到 https://domain