views:

227

answers:

4

I have a challenge using Apache.

In my .htaccess file I'd like to convert requests like this:

url/portfolio/filename.htm

to:

url?filename

Any takers? Thanks for your time

A: 

Something like this should work, although I have not tested it.

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)/portfolio/(.*)\.htm$ $1?$2 [PT,L]


</IfModule>
<IfModule !mod_rewrite.c>
    ErrorDocument 404 index.php
</IfModule>
Alec Smart
Alec, thanks very much for the quick response. I tried it and it doesn't seem to work, so I used a regex tester to see what might be up...is there chance of a 'delimiter collision' with this? Thx D
+1  A: 

You have a couple of choices, depending on how you want the URL to appear to visitors (and search engines).

If you want the externally visible URL to remain url/portfolio/filename.htm, Alec's solution worked after I removed the two RewriteCond lines.

RewriteEngine On

RewriteRule ^(.*)/portfolio/(.*)\.htm$ $1?$2 [PT,L]

If other parameters could be in the query string and you want to preserve those, add QSA to the options in brackets at the end of the rule.

If you want people outside to see url?filename instead, change the rule to:

RewriteRule ^(.*)/portfolio/(.*)\.htm$ $1?$2 [L,R]

Same qualification about other query parameters applies.

If this still doesn't help, I suggest you turn on rewrite logging and look in that log for more clues. Post them here and someone will help. You might have to put this part in httpd.conf. My Apache didn't like it in .htaccess.

RewriteLog ...path...
RewriteLogLevel 3  # you'll regret anything higher
Steve Madsen
A: 

This should work:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^portfolio/(.+)\.htm$ index.php?$1 [NC,L]

or if you want to visibly change it:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^portfolio/(.+)\.htm$ /?$1 [R,NC,L]

The second one would look like example.com/?filename to the user, whereas the first one would look like example.com/portfolio/filename.htm.

Hope that helps.

Sam
A: 

Hello all and thanks for your replies. I can't add a comment to your replies directly for some reason (I could the other day!) so I'll add it here:

Gumbo, thanks for your reply...the 'portfolio' is constant. In this form: http://www.myurl.com/portfolio/picturename.htm

I'd like it to be changed to:

..com?picturename

(had to edit out the second hyperlink, forum rules)

Sam and Steve, thanks for your replies as well. Unfortunately nothing seems to work...including the http.conf. Not that there's an error message, just that nothing happens.

Steve, is this the right format for my path in httpd.conf?

RewriteLog "/myurl.com/httpdocs/logs.log" #leading slash, no http or www RewriteLogLevel 3

THANKS d