Php 漂亮的URL条件重写
我们的网站允许一个漂亮的url“etest.me/1234”,其中“1234”是一个客户端id。正如您在下面看到的,它重定向到我们的route.php文件,我们在那里重定向。问题是,当客户端使用“etest.me”而不使用“/1234”时,它们会收到“请求的URL/go/在此服务器上找不到”消息。当缺少“/1234”时,我希望url转到另一个页面 请注意,我们已将域和路径转发到不存在的“/go”目录,因此下面的规则将捕获它。下面是根目录中的.htaccess文件Php 漂亮的URL条件重写,php,apache,.htaccess,mod-rewrite,Php,Apache,.htaccess,Mod Rewrite,我们的网站允许一个漂亮的url“etest.me/1234”,其中“1234”是一个客户端id。正如您在下面看到的,它重定向到我们的route.php文件,我们在那里重定向。问题是,当客户端使用“etest.me”而不使用“/1234”时,它们会收到“请求的URL/go/在此服务器上找不到”消息。当缺少“/1234”时,我希望url转到另一个页面 请注意,我们已将域和路径转发到不存在的“/go”目录,因此下面的规则将捕获它。下面是根目录中的.htaccess文件 RewriteEngine On
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
您可以添加新规则来处理此情况:
RewriteRule ^go/$ ./another_script\.php
对我来说工作很好。希望这对你也有用
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
我接受了Sahil的回答,并在下面发布了完整的逻辑供其他人使用:
Domain: etest.me
Pretty URL: etest.me/1234 (where '1234' is a client id)
Forwarded to: etesting.io/go (with forward path option selected so the '/1234' will be include in the forward)
注意:服务器上没有“/go”目录,“/go”用于触发重定向
希望“etest.me”(不带客户端id)转到根目录中的索引文件
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
以下是.htaccess中的重写规则:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^go/([a-zA-Z0-9/]*)$ ./route\.php?go=$1
下面是route.php代码:
<?php
if($_GET['go']){
$a = explode('/', $_GET['go']);
$c = count($a);
if($c == 1 || $c == 2){
header('location: ../../index.php?aid=' . $a[0] . '&tid=' . $a[1]);
exit;
}
else{
die('Invalid URL');
}
}
else{
header('location: ../index.php');
exit;
}
希望这对将来有帮助
我想要一个更优雅的解决方案,但它现在已经过时了…如果没有
客户端id
,您想重定向到哪里?index.php位于.htaccess所在的根目录中目录/go不存在。它仅在转发域etest.me=>etesting.io/go where/go中用于触发重写失败。目录/go不存在。它仅在转发域etest.me=>etesting.io/go where/go中用于触发重写(如上面的注释所述)。如果没有转发/goOr,我想我做不到。你可以尝试重写规则^go/([a-zA-Z0-9/]*)$./route\.php?go=$1
并在route.php脚本中处理空的$\u GET['go'](更改为*)仍然希望使用/go'此服务器上找不到请求的URL/go/index.php。'需要删除/go部分,因为现在没有/go directory.works。不得不将route.php更改为header('location:../index.php');(添加“../”以退出go dir返回路线。我认为上面的内容将满足您的所有需要