开发者

PHP alter URL without redirecting to it?

开发者 https://www.devze.com 2023-02-13 04:12 出处:网络
How ca开发者_开发知识库n I alter url or part of it using php? I lost the code that I have used and now I cannot find answer online.

How ca开发者_开发知识库n 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.


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 ;)


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.


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)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消