1

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?

2
  • It's not clear what you are trying to do here, can you provide some examples with URLs and what resource they should map to? Is the only think your index.php script doing is redirecting to http://192.167.1.189/site_name/? Is the htaccess file sitting in 192.167.1.189's document root? Commented Sep 27, 2012 at 20:11
  • htaccess is in the root and is functioning. Index has some code after redirect but I will have a dedicated route file just for routing later(just testing now). Updating the Q to add url examples Commented Sep 27, 2012 at 20:28

1 Answer 1

1

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:

  1. Browser requests a friendly URL
  2. Rewrite Rules in .htaccess rewrite the friendly to the one with the ?route= query string
  3. The index.php script takes the request, and processes it, and gives the browser what it wants

But you want to also do:

  1. Browser requests an ugly URL with the ?route= query string
  2. Rewrite Rules do nothing because the URI doesn't match
  3. The index.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/.

Sign up to request clarification or add additional context in comments.

4 Comments

It's close but I don't need ugly url's to work(they should redirect to 404 handler). I only need full filenames for images(.jpg,.png etc...) and for css, javascript, php include files. Since most links need to be redirected to index.php how can I prevent infinite looping? From what I can gather your suggestion will still result in redirect loop.
@DominicM It shouldn't because you first check if the request was for something starting with /site_name/movie/, if it doesn't then execute what's in the if block, which just redirects to /site_name/movie/. If the request starts with /site_name/movie/, it won't redirect (which is what's causing the loop).
That makes sense, will try this out over next couple of days and get back with the results, thanks.
Actually it doe not and cannot work. What I was doing is redirecting all requests to index.php and then redirecting again to a requested page. I is not possible to redirect with php to a friendly url while displaying contents of an ugly url. Instead of header redirect, I should have used include! Accepting your answer anyway, thanks. f you have any suggestions on excluding images and css files from redirects let me know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.