0

如何创建一个实际上并不存在的 URL?例如,https://twitter.com/i/notifications - 有效。当您从链接中删除/notifications时,您会得到https://twitter.com/i/ - 不起作用。但是这个 URL /i/并不存在。

我怎么做这样的东西?

4

3 回答 3

1

您可以通过编辑.htaccess文件来使用 URL 重写。如果.htaccess不存在,那么您可以在项目根目录中创建一个。

RewriteEngine on
RewriteRule ^/?Some-text-goes-here/([0-9]+)$ /picture.php?id=$1

以上将转换url.com/picture.php?id=51picture.php/Some-text-goes-here/51

以同样的方式,你可以使用你的策略。

于 2017-01-06T19:35:56.000 回答
1

您可以创建一个 .htaccess 文件,通过单个 PHP 文件路由所有请求,然后使用该 PHP 文件中的逻辑来确定 url 是否存在。例如,您可以使用我的 CodeIgniter .htaccess 文件:

RewriteCond $1 !^(index\.php|images|css|fonts|js|robots\.txt|test)
RewriteRule ^(.*)$ /index.php/$1 [L]

这使您仍然可以指定不会重新映射的图像、css、js 等,但任何不以这些字符串开头的 url 都将被路由到 index.php。在 index.php 中,您可以检查查询字符串以确定如何处理 url 并确定它们是否存在。

// code in index.php, modify to suit
switch ($_SERVER["PATH_INFO") { // you might also check out REQUEST_URI
  case "foo":
    // do blah blah blah
    break;
  case "bar":
    // do other blah blah blah
    break;
  default:
    header("HTTP/1.0 404 Not Found");
    echo "Sorry but that page was not found";
}
于 2017-01-06T19:27:55.587 回答
1

感谢大家的帮助!

最终解决方案:RewriteRule ^/?i(/.*)?/company /company [L,NC]

于 2017-01-06T20:02:22.383 回答