user1142052 user1142052 - 5 months ago 24
PHP Question

friendly url not showing in location bar after php redirect

I am using mod-rewrite to redirect friendly URLs to a php script that looks up the actual URL in an associative array and then redirects with header("location: $url");

My rewrite rule is

RewriteEngine On
RewriteCond %{REQUEST_URI} "^/$"
RewriteRule ^(.*) http://%{HTTP_HOST}/index.php [L,R=301]
RewriteCond %{REQUEST_URI} !=/home.php
RewriteCond %{REQUEST_URI} !(\.php|\.html|\.htm|\.png|\.gif|\.css|\.htm|\.js|\.txt|\.xml)$ [NC]
RewriteRule ^([^/]*)$ /home.php?page=$1 [L]

This results in the $url appearing in the browser's location bar whereas I want the friendly url to appear.

I understand from this post Rewriting URL and making new URL show in location bar why this occurs (because of the php location header) but I don't know how to achieve what I want.

There are a large number of dynamic pages that are built by several internal php scripts and writing mod-rewrite rules for all combinations of script and request parameters would be difficult.

So my questions is, how can I make the friendly url appear in the location bar.


In your PHP script instead of redirecting the user using

header("location: $page");

you need to include that page. You'll definitely want to first ensure that the value of $page is an accepted value:


// /home.php

$page = $_GET['page'];

$valid_pages = array(

if(in_array($page, $valid_pages)){
   include($page . '.php');

die('Invalid Page.');