How to re-direct URL?

Hi guys, I would like a capability so that when someone visits these URLs,

http://www.domain.com/somedir/xyz.php
http://www.domain.com/somedir/info.php

they will be respectively re-directed to:

http://www.domain.com/somedir/main.php?p=xyz
http://www.domain.com/somedir/main.php?p=info

Any idea on how to do it in Apache or PHP ? Thanks.

What you’re looking for is Apache’s mod_rewrite. Here’s a great beginner’s article on URL rewriting from a pretty cool site, but here’s the basic gist:

You use regular expressions to parse the URL that the user requested, and tell the server which page it should actually send.

If you have mod_rewrite enabled on your server, you can just put the following in an .htaccess file located in your somedir folder:


RewriteEngine on
RewriteRule ^/?([a-z]+)\\.php$ main.php?p=$1

Thanks sdleihssirhc for your prompt reply. I’m a bit concerned with your regular expression though. I want only the files located under the “somedir” directory to be re-directed and not the files located outside of this directory. For example, when someone visits this link:

http://www.domain.com/somedir/info.php

it will be re-directed to: http://www.domain.com/somedir/main.php?p=info

But when he visits this site:

http://www.domain.com/somepage.php

it should not be re-directed.

Does your regex cover this case as well ?

Thanks a lot again.

Just make sure that you put your .htaccess file inside the somedir folder and you should be okay.

Ah, sdleihssirhc, your solution is “loopy.” You always must check the regex to see whether it’ll match the redirection, too, and, if it does, provide an exit strategy (with a better regex OR a RewriteCond).

Regards,

DK

Can you explain what about the rule puts it at risk for looping? I tried accessing main.php, thinking that maybe the regex doesn’t take parameters into account (so if you accessed main.php, it would try to load main.php?p=main, which would trigger with main.php again), but everything seemed to work fine.

sdleihssirhc,

Sure. Let me lead you through your code:

RewriteEngine on
RewriteRule ^/?([a-z]+)\\.php$ main.php?p=$1

RewriteEngine on is perfect.

The RewriteRule’s regex will match (Apache 1 or Apache 2 - good!) a lowercase letter filename with a php file extension. It will then redirect to main.php with a query string.

The problem is that main.php will be matched by {filename}\.php and redirected to main.php with a query string of p=main … until Apache 2 recognizes the loop or Apache 1 gets a restart.

Note 1: Because .htaccess is a per-directory resource, Apache will be looking for (and matching) filename.php then main.php then main.php then …

Note 2: This is very similar to the effect that (.*) has when redirecting to the same directory with the only difference being that you are aware enough to use good regex in your RewriteRule (although, these days, the /? isn’t necessary because there are very few Apache 1.x servers remaining).

You’ve done a good job (with the exception of the loop), though. My solution would be very similar:

RewriteEngine on
# Prevent the loop by requiring that there be no query string, i.e.
# RewriteCond %{QUERY_STRING} ^$
# OR that the file requested is not main.php (very slight preference)
RewriteCond %{REQUEST_URI} !main\\.php$
# I prefer my .htaccess to be in the DocumentRoot so I've got to include the somedir
RewriteRule ^somedir/([a-z]+)\\.php$ somedir/main.php?p+$1 [L]

I hope that answers your question (and provides a useable solution for tmyonline).

For tmyonline, may I offer the tutorial Article located in my signature – it’s helped members here for years.

Regards,

DK

Thanks guys, I feel good to have two experts, dklynn and sdleihssirhc, helping me.

Sure dklynn, where can I find it ? Thanks.

tmyonline,

Try clicking on the word “Article” in my signature.

Regards,

DK