.htaccess redirect a url to PHP using RewriteRule

David I

Trying to setup .htaccess so I can redirect a url like http://example.com/io/abc123 to http://example.com/io/io.php?id=abc123. But, for some reason, the RewriteRule does not work as I expect.

I want to keep this redirection local without messing up the root .htaccess file, and so I create a new .htaccess file inside the io subdirectory. (Doing the equivalent in the root directory gives the same outcome, so it's not that).

Directory structure:

www
 |
 +--io
    |
    +-- .htaccess
    \-- io.php

.htaccess file:

RewriteEngine on
RewriteRule ^(.*)$ io.php?id=$1

io.php file:

<?php
  echo "Hello World" . "\n";
  echo "REQUEST_URI=[" . $_SERVER['REQUEST_URI'] . "]\n";
  echo "PHP_SELF=[" . $_SERVER['PHP_SELF'] . "]\n";
  echo "QUERY_STRING=[" . $_SERVER['QUERY_STRING'] . "]\n";
  $id = $_GET['id'];
  echo "id='" . $id . "'\n";
  ?>

OUTPUT

Hello World
REQUEST_URI=[/io/abc123]
PHP_SELF=[/io/io.php]
QUERY_STRING=[id=io.php]
id='io.php'

For reasons I don't understand, instead of the expected query_string id=abc123 this results in id=io.php.

It's obviously getting the rewrite correct to the target file io.php, but fails to substitute the abc123 into the id parameter using $1.

What am I doing wrong?

anubhava

For reasons I don't understand, instead of the expected query_string id=abc123 this results in id=io.php

Reason is that your rule is executing twice since there is no pre-condition to not to rewrite for existing files and directories.

  1. First time it matches abc123 and rewrites to io.php?id=abc123
  2. Now our REQUEST_URI has become io.php. mod_rewrite runs in a loop unless there is no executing rule or else if source and target URIs are same. So second time it matches io.php (since our pattern is .*) and it gets rewritten to io.php?id=io.php.

mod_rewrite engine stops here because source and target URIs are same.

To fix this issue, you should use:

RewriteEngine On

# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ io.php?id=$1 [L,QSA]

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Using redirect 301 or rewriterule in htaccess to redirect and take a php variable with you

How to ReWriteRule and then Redirect 301 it using .htaccess

htaccess | How to redirect using RewriteRule ? Simple

htaccess RewriteRule redirect regex

.htaccess RewriteRule Redirect

htaccess 301 redirect to rewriterule

.htaccess RewriteRule to redirect http to https messes up % escaped parameters in URL

How to redirect to clean URL (remove last hypen) with RewriteRule in .htaccess

redirect url if it contain a specific words using .htaccess and php

PHP/Htaccess url redirect/mapping

How to exclude .php file from .htaccess redirect(rewriteRule)?

Selective url redirect using .htaccess

Redirect registration url using .htaccess

.htaccess RewriteRule with slash in url

The htaccess RewriteRule for my redirect is not working

.htaccess RewriteRule to redirect all to root

.htaccess 301 redirect conflicting with rewriterule

Redirect to php script using htaccess

How to redirect URL into a new URL using .htaccess?

Redirect to url within another url using .htaccess

Remove .php extension in .htaccess with already working friendly-url RewriteRule

.htaccess - How to redirect URL to a folder and then to a PHP file

redirect htaccess with php variables and rewrite url

301 Redirect A URL Pattern Using .htaccess

Redirect a Mod_Rewrite URL using .htaccess

How to redirect URL in codeignator using htaccess?

Redirect one URL to another using htaccess

301 redirect with multiple URL's using .htaccess

redirect url from pagination using htaccess