htaccess problem.
January 11, 2004 2:51 PM   Subscribe

Will the lines [shown inside] in the .htaccess file cause ANY url of the form www.example.com/filename to be matched and rewritten to www.example.com/m2.php?pg=filename?

The best I could come up with, which don't work, are:

RewriteEngine on
RewriteRule ^(.*)$ m2.php?pg=$1 [T=application/x-httpd-php]
RewriteEngine off

I'm a complete newby at this but am trying to get URLs that match specific keywords. A later evolution will require secondary matching like

www.example.com/category/subtopic

to

www.example.com/m2.php?pg=category&pg2=subtopic
posted by billsaysthis to Computers & Internet (15 answers total)
 
Looks like you just have an extra $ sign in the middle.

I use the following line, which works, to match /search/filename to an argument of mt-search.cgi:

RewriteRule ^blog/search/(.*) /mt/mt-search.cgi?IncludeBlogs=2&search=$1
posted by Aaorn at 3:02 PM on January 11, 2004


Unless I'm horribly wrong, that "RewriteEngine off" line disables all rewrite rules. Try removing it. The search pattern itself looks OK to me.
posted by reynaert at 3:32 PM on January 11, 2004


Looks like you just have an extra $ sign in the middle.

Nah, that's a special regex char that means end of line. So he's capturing everything from the beginning (^) to the end ($) of the line.

Anyway, with that regex even redirected requests will get caught. Take a look at the apache mod_rewrite doc for the RewriteCond directive.
posted by thebabelfish at 3:46 PM on January 11, 2004


Here's what you want:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !m2.php
RewriteRule ^(.+)$ /m2.php?pg=$1


I don't think you need the [T=application/x-httpd-php] at all, but haven't tested it. If you want the redirect to be transparent, tag a [P] on the end of the rule (with a space between the second arg).
posted by thebabelfish at 3:59 PM on January 11, 2004


aaorn, reynaert, babelfish, thanks. The result, with or without the $ in the middle of the line or using the RewriteCon version, is that only my 404 page comes up and without using the stylesheet specified being used. No matter which page or URL I try and bring up in the browser. I did look at the Apache docs but can't really translate it into englishbillspeaks.
posted by billsaysthis at 4:06 PM on January 11, 2004


Erm. Make sure that m2.php actually exists in Apache's DocumentRoot. If it does, I can't really help much without access to the server. The version I posted works for me on my server.
posted by thebabelfish at 4:27 PM on January 11, 2004


You need something to differentiate the dynamic pages from the real files.

Try this:

# first try to see if file exists, if it does let it through
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.+) $1 [L]

# else go on to m2.php
RewriteRule ^(.+)$ /m2.php?pg=$1 [L]
posted by riffola at 4:35 PM on January 11, 2004


tbf: uggh, m2.php most certainly does exist

riffola: with your version the stylesheet is picked up and calling m2.php (including calling it with the parameter) does show up properly. But the other way, www.example.com/filename, still gets the 404.
posted by billsaysthis at 5:27 PM on January 11, 2004


Try [PT] instead of [L].
posted by riffola at 5:52 PM on January 11, 2004


*screams incoherently* still no luck, mr. riffman.
posted by billsaysthis at 6:04 PM on January 11, 2004


Ok, I think part of the problem is that you need a virtual cruft in the URL.

For example:

http://www.metafilter.com/mefi/1449

Whereas the original url used to be:

http://www.metafilter.com/detail.cfm?link_ID=1449

the mefi part in the URL acts as an alias (I think)

So you'd need a similar alias directory in the URL.

If that is not an option just use brute PHP :)

// get params from path_info:
list($dummy, $pg) = explode('/', $_SERVER["PATH_INFO"]);

It'll grab $pg from the URL.

Also throw in a PHP.net style error page on the index page to handle your errors.
posted by riffola at 6:15 PM on January 11, 2004


riffola, thanks for the continuing effort but, as far as the latter, I already have the PHP brute force approach working; that's where the m2.php?pg=filename comes from.
posted by billsaysthis at 6:50 PM on January 11, 2004


Since you say this is in a .htaccess file, you'll need a RewriteBase directive.

In your case, probably "RewriteBase /". That in turn will strip the "/" from the beginning of the request URI during rule matching, so you'll have to modify your rules slightly.
posted by Khalad at 8:46 PM on January 11, 2004


Correction, replace "you'll need" by "you should try." I always thought RewriteBase was required in .htaccess files, but apparently I was wrong.

Also, as a general mod_rewrite debugging tip, you should enable the log, set it to level 9, and see what exactly mod_rewrite is doing. It will tell you in great detail what matches it makes and what substitutions it performs.
posted by Khalad at 8:53 PM on January 11, 2004


FWIW, adding the line "RewriteBase /" makes no difference either. How frustrating but the help is appreciated.
posted by billsaysthis at 12:53 PM on January 12, 2004


« Older Several questions about developing museum exhibits...   |   CDRW worked in Win98SE, inoperable in XP Newer »
This thread is closed to new comments.