Web Hosting Talk







View Full Version : .htaccess url rewrite help needed.


acctman
05-31-2008, 11:25 AM
Hi can anyone assist in helping me rewrite the following url.
http://www.domain.com/view/UserName.html to
http://www.domain.com/UserName

Question if a user makes a name called Temp for there username and I have a temp folder on the server would there be a conflict or would the rewrite only work if the user name is formated without the trailing /

thanks

acctman
06-01-2008, 12:54 AM
is there a htacess re-write generator i can use?

isurus
06-01-2008, 06:05 PM
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html

redirect:
RewriteEngine on
RewriteRule ^/view\/(.*).html$ $1 [R]Proxy:
RewriteEngine on
RewriteRule ^/view\/(.*).html$ $1 [P]
Note, to use apache as a proxy you will need to have enabled its proxy capabilities...

Question if a user makes a name called Temp for there username and I have a temp folder on the server would there be a conflictThe rewrite rule will simply map http://www.domain.com/view/Temp.html (http://www.domain.com/view/UserName.html) to http://www.domain.com/Temp

ie you will get the same behaviour as though you had gone to http://www.domain.com/Temp

If you want to, you could use a RewriteCond directive to stop the rewrite rule working on specific paths:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !^/view/Temp.html
RewriteRule ^/view\/(.*).html$ $1 [R]Perhaps it may be useful to have a rewrite rule that diverts certain reserved usernames to a warning page?
eg http://www.domain.com/view/Temp.html -> http://www.domain.com/invalid_username.html
eg http://www.domain.com/view/admin.html (http://www.domain.com/view/Temp.html) -> http://www.domain.com/invalid_username.html
etc

I don't know anything about the rest of your site, so am just guessing....

HTH


Incidentally, you should just treat the examples above as pointers to get you started - they need some tweaking. For example, if you use the proxy option, and someone went to
http://www.domain.com/view/http://www.dodgy.com.html (http://www.domain.com/view/http://www.phishing_site.com.html) then http://www.dodgy.com (http://www.phishing_site.com) would be served to the user, apparently from your domain.... iirc, there was a phishing scam that exploited a vulnerability like this on the paypal website a few years ago.

Richardmtl
06-02-2008, 11:14 AM
Just a quick reminder for the OP (sorry if this is basic, but I learned the hard way!):

Empty your cache if you're testing it out. I had to do this (on FF) to see the changes I did when playing with htaccess. I'm not sure of the technical reason why, but that's how it was, in any case. Good luck!

Tim Greer
06-03-2008, 08:05 PM
If you were unable to figure the solution out, then please post your current rewrite rules for us to see and we'll (us here at WHT) be able to help get it squared away.

acctman
07-28-2008, 07:01 PM
If you were unable to figure the solution out, then please post your current rewrite rules for us to see and we'll (us here at WHT) be able to help get it squared away.

this is my current mod rewrite. i was not able get the original example to work correctly


DirectoryIndex index.php
Options -Indexes
Options +FollowSymLinks

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^ads/
RewriteCond %{REQUEST_URI} !^shop/
RewriteCond %{REQUEST_URI} !^mp3/
RewriteCond %{REQUEST_URI} !^swf/
RewriteCond %{REQUEST_URI} !^js/
RewriteRule ^(.*) index.php [L]
</IfModule>

php_flag register_globals on

acctman
07-29-2008, 07:08 PM
can anyone assist me with the correct changes to make to my .htaccess

foobic
07-29-2008, 07:57 PM
Hi can anyone assist in helping me rewrite the following url.
http://www.domain.com/view/UserName.html to
http://www.domain.com/UserName
I'm wondering if perhaps what you really want is the opposite of the rewrite you asked for, ie. A visitor types in the url example.com/UserName and this gets rewritten internally to your actual page at example.com/view/UserName.html. Would that be correct?

acctman
07-30-2008, 12:20 PM
I'm wondering if perhaps what you really want is the opposite of the rewrite you asked for, ie. A visitor types in the url example.com/UserName and this gets rewritten internally to your actual page at example.com/view/UserName.html. Would that be correct?

yes you are correct. do you know how I would do this?

foobic
07-30-2008, 07:08 PM
That's a bit trickier because you need to check whether the view/UserName.html file exists and if not let the request fall through to your other rules.

I think this should work:

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^/(.*)
RewriteCond %{DOCUMENT_ROOT}/view/%1.html -f
RewriteRule .* /view/%1.html [L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^ads/
RewriteCond %{REQUEST_URI} !^shop/
RewriteCond %{REQUEST_URI} !^mp3/
RewriteCond %{REQUEST_URI} !^swf/
RewriteCond %{REQUEST_URI} !^js/
RewriteRule ^(.*) index.php [L]
</IfModule>

lexington
07-30-2008, 08:54 PM
I recently came across the following forum on Google and the guy answered my question quickly as if I was paying him. I just wanted to give his site credit for helping me with my mod rewrite questions so you should check out:

http://forum.modrewrite.com/