PDA

View Full Version : php url redirection


jmontecillo01
20th July 2011, 12:34 AM
I am not sure though if the requirement I have is called "redirection". This is what the problem is all about.

If I type in www.mysite/index.php/searchitem (http://www.mysite/index.php/searchitem), the page index.php will be executed with:

$_SERVER['REQUEST_URI']
"/index.php/searchitem"

$_SERVER['SCRIPT_NAME']
"/index.php"

But if I request www.mysite/searchitem (http://www.mysite/searchitem), index.php will be executed with

$_SERVER['REQUEST_URI']
"/searchitem"

$_SERVER['SCRIPT_NAME']
"/index.php"

I then simply match 'REQUEST_URI' and 'SCRIPT_NAME' to figure out what is being requested.

This url format works under apache by adding in .htaccess:
RewriteRule . index.php

I could not make the second example (the one without index.php) work under IIS. It keeps on telling me that the path/page I requested does not exists.

In fact $_SERVER['REQUEST_URI'] is not provided under IIS. I had to check if $SERVER['REQUEST_URI'] is set. and if not, then I set in manually.

Thanks in advance for any advice.

erlando
20th July 2011, 01:06 AM
Try adding a '/' :

RewriteRule . /index.php

ETA: You'll probably want to add QSA as a flag to that

jmontecillo01
20th July 2011, 02:37 AM
Thanks,

It works now. The only thing is for apache, the file .htaccess is used while in IIS, the file is web.config.

Just in case others are interested,

(Apache) .htaccess:

Options +FollowSymLinks
IndexIgnore */*
# Turn on the RewriteEngine
RewriteEngine On
# Rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php

for IIS - web.config

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<system.web>
<identity impersonate="false" />
</system.web>
<system.webServer>
<rewrite>
<rules>
<rule name="Imported Rule 1" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions>
<add input="{URL}" pattern="^/webitor/api.*$" ignoreCase="false" />
</conditions>
<action type="Rewrite" url="webitor/webitor_api.php" appendQueryString="true" />
</rule>
<rule name="Imported Rule 2" stopProcessing="true">
<match url="^(.*)$" ignoreCase="false" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" ignoreCase="false" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" ignoreCase="false" negate="true" />
<add input="{URL}" pattern="^/webitor/api.*$" ignoreCase="false" negate="true" />
</conditions>
<action type="Rewrite" url="index.php" appendQueryString="true" />
</rule>
</rules>
</rewrite>
<directoryBrowse enabled="false" />
</system.webServer>
</configuration>

ETA: don't know why some words are suppressed by rule 2.