How to Capture Specific URL Segments for 301 Redirects on Windows/IIS Servers with PHP?

DDD
Release: 2024-10-30 01:16:28
Original
440 people have browsed it

How to Capture Specific URL Segments for 301 Redirects on Windows/IIS Servers with PHP?

Retrieving the Current Page URL on Windows/IIS Servers for PHP Redirects

In scenarios where WordPress installations are migrated to new folders on Windows/IIS servers, setting up 301 redirects in PHP encounters challenges in capturing the URL's specific segment. This article explores the issue and provides a solution for obtaining the desired URL portion.

The Problem: Empty Strings and Truncated Values

Commonly suggested solutions, such as using $_SERVER["REQUEST_URI"], yield empty strings on IIS servers. Similarly, $_SERVER["PHP_SELF"] only returns "index.php," omitting the crucial "/post-title/" segment of the URL.

The Solution: Harnessing $_SERVER['PATH_INFO']

IIS servers provide an alternative variable, $_SERVER['PATH_INFO'], which resolves this issue effectively. This variable retrieves the requested resource's path relative to the server's installed root directory.

In the example provided, where URLs follow the format "/OLD_FOLDER/index.php/post-title/", $_SERVER['PATH_INFO'] would yield "/post-title/". This value can then be utilized to construct the desired redirect paths.

Conclusion

By leveraging $_SERVER['PATH_INFO'] in IIS environments, developers can accurately capture the URL segments required for 301 redirects, ensuring seamless website migrations and proper redirection of traffic from old to new URLs.

The above is the detailed content of How to Capture Specific URL Segments for 301 Redirects on Windows/IIS Servers with PHP?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!