I have a .htaccess file with the following rules:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]
In the index.php I have:
$route = (isset($_GET['route'])?explode('/',$_GET['route']):null);
if(!empty($route)){
$route[0]; //movie
//$route[1]; //movie-name
header("Location: " . "http://192.167.1.189/site_name/movie/" . $route[1]);
// $route[1] = 2271 (movie id)
exit;
}
The goal is to get from this url:
http://192.167.1.189/site_name/movie-page.php?id=2270
to this url:
http://192.167.1.189/site_name/movie/2270
I will also want some other url changes such as .../movie-page.php to .../movie/ and so on...
I understand why I get the redirect loop, but I don't know how to fix it. How can I redirect all (except valid files and directories such as css files and images) to a central php file and then redirect to the user friendly url?
I understand why I get the redirect loop, but I don't know how to fix it. How can I redirect all (except valid files and directories such as css files and images) to a central php file and then redirect to the user friendly url?
So it sounds like this is what you're trying to do:
?route=
query stringindex.php
script takes the request, and processes it, and gives the browser what it wantsBut you want to also do:
?route=
query stringindex.php
script takes the request, sees that it's an ugly URL, and redirects the browser to the friendly one.In order to do this, you need to check in your script what the origially requested URL looked like. You can do it by looking at the $_SERVER['REQUEST_URL']
or the $_SERVER['REQUEST_URI']
variables. They should tell you the URI of what the browser actually requested.
For example, if someone puts in their URL address bar in their browser the URL: http://192.167.1.189/site_name/movie/2270
, then the rewrite rule will route that to index.php
and then in your php file you can check the $_SERVER['REQUEST_URI']
and it will say: /site_name/movie/2270
.
But if someone puts in their URL address bar: http://192.167.1.189/site_name/movie-page.php?id=2270
, then the $_SERVER['REQUEST_URI']
will be /site_name/movie-page.php?id=2270
. So maybe you want something like this in your if statement:
if(!empty($route) &&
strpos($_SERVER['REQUEST_URI'], "site_name/movie/") != 1
){
So you want to make sure there's something in the $route
var and that the $_SERVER['REQUEST_URI']
var doesn't start with /site_name/movie/
.