PHP alter URL without redirecting to it?

2019-08-05 16:14发布

How can I alter url or part of it using php? I lost the code that I have used and now I cannot find answer online.

I know using header('Location: www.site.com') gets redirect but how can I just show fake URL?

I don't want to use mod_rewrite now.

4条回答
男人必须洒脱
2楼-- · 2019-08-05 16:57

You can make somewhat SEO-friendly URLs by adding directories after the php script name so that your URLs become:

http://yoursite.com/script.php/arg1/arg2

In script.php:

<?php

$args = preg_split('#/#', $_SERVER['PATH_INFO']);

echo "arg1 = ".$args[1].", arg2 = ".$args[2]."\n";

?>

if you use some more htaccess trickery, then you can make the script.php look like something else (see @David's answer for an idea)

查看更多
Animai°情兽
3楼-- · 2019-08-05 17:08

You can try using,

file_get_contents("https://website.com");

This is not going to redirect but fire the api and you can catch the output by assigning a variable to above function.

查看更多
Deceive 欺骗
4楼-- · 2019-08-05 17:11

It is impossible in PHP, which is executed server side. Any change to the url you make will trigger a page loading.

I think it may be possible in javascript, but I really doubt this is a good idea, if you want to rewrite an url only in the user adressbar, you're doing something wrong, or bad ;)

查看更多
成全新的幸福
5楼-- · 2019-08-05 17:19

What you've actually asked for isn't possible in using PHP (Although, in JavaScript you can use the dreadful hashbang or the poorly supported bleeding edge pushState).

However, in a comment on another answer you stated that your goal is actually friendly URIs without mod_rewrite. This isn't about showing a different URI to the real one, but about making a URI that isn't based on a simple set of files and directories.

This is usually achieved (in PHP-land) with mod_rewrite, so you should probably stick with that approach.

However, you can do it using ScriptAlias (assuming you use Apache, other webservers may have different approaches).

e.g. ScriptAlias /example /var/www/example.php in the Apache configuration.

Then in the PHP script you can read $_SERVER['REQUEST_URI'] to find out what is requested and pull in the appropriate content.

查看更多
登录 后发表回答