简体   繁体   中英

Redirecting Urls via PHP without htaccess

I want to redirect my urls but not with htaccess, only with php. I know that I have to use the header() function. But my question is how to catch the url.

For example, Wordpress catches urls like mysite.com/postname and redirects it to other urls, I think it is index.php?parameters=values .

But my question how to catch the url mysite.com/postname and redirect it to other. Which php script will catch it.

Or when wordpress catch the url, which php file redirects it to index.php

You need to tell your HTTP server that the URL is handled by the PHP script.

You can't do this with PHP directly: If the server never runs the PHP script, then the PHP script can't do anything with the request!

This is most commonly done with mod_rewrite which is configured using Apache configuration. There are two basic places that you can put mod_rewrite directives.

The former is recommended:

You should avoid using .htaccess files completely if you have access to httpd main server config file. Using .htaccess files slows down your Apache http server. Any directive that you can include in a .htaccess file is better set in a Directory block, as it will have the same effect with better performance.

… and you've rejected .htaccess so put the directives in your main configuration file.


This, of course, assumes you are using Apache HTTPD. You didn't say which HTTP server you were using.


This also assumes that when you rejected .htaccess you didn't mean mod_rewrite . Many people conflate the two since changes to mod_rewrite settings are the most common things that programmers (as opposed to System Administrators) want to do in an Apache configuration.


For example, Wordpress catches urls like mysite.com/postname and redirects it to other urls

Wordpress uses mod_rewrite directives in a .htaccess file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM