Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite url rewriting

using directory type rewriting

         

mikesoft

8:50 pm on Jul 18, 2010 (gmt 0)

10+ Year Member



Hey all,

Well I have this issue with my htaccess url rewriting.

I wanted my web app to hide the php extension and at the same time get rid of dynamic URLs, converting them to static using mod_rewrite.

I studied a few options available, first one I tried was:

Options +FollowSymLinks
RewriteEngine on
RewriteRule profile-(.*)\.htm$ profile.php?id=$1

which would redirect http://mysite.com/profile-1.htm to profile.php?id=1

That one works amazing, however, I kinda like the directory type of rewriting, like this:

Options +FollowSymLinks
RewriteEngine on
RewriteRule profile/(.*) profile.php?id=$1

That one would redirect
http://mysite.com/profile/1
to
http://mysite.com/profile.php?id=1

That one works too. However, I notice that when I reach any profile page, it loads ok but you can see at the status bar that the browser is somehow on a loop waiting and transferring from the website really fast. This happens for like 6 seconds and then it says done. But with the first type of rewriting (the htm method) the page loads instantly and it says done almost instantly as well.

I want to know what is wrong with my htaccess for the directory type of rewriting, what can I do to solve this? I sure don't want to use the htm method, I don't like the way URL looks, so I rather not use any rewriting at all if I can't use the directory rewriting method.

Please help!

jdMorgan

9:40 pm on Jul 18, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There's nothing wrong with that code, except that I recommend putting an [L] flag on every rule unless you know why you don't want one...

If there is a problem here, it is likely caused by an iteraction with some other rewriterule, Redirect, or RedirectMatch directive, or one of several other directives that could also interact with rewritten URLs...

BTW, neither of those two rules is a redirect. They are both URL-to-internal-filepath rewrites.

Jim

mikesoft

1:54 am on Jul 19, 2010 (gmt 0)

10+ Year Member



Thanks, I added the [L] to all rules now. I'll check up later what's that for.

In the meantime, I took the rewriting stuff a step forward.

Now I wanted to have URLs like "mysite.com/login" but of course, without the use of actual directories, but with the mod_rewrite.

So I added this line to htaccess:
RewriteRule login index.php?mode=login [L]


which works alright, however, I've noticed that several files named "login" would not work anymore. For example, an image used as button named "login.gif" would no longer work on my site (doesn't show up, instead the 'alt' property appears). I had to rename it to log.gif in order for it to work. Is there a way to prevent this kind of behavior? Or is this the drawback for using mod_rewrite like this?

Basically I want to have several pages like this, not just the login page.

RewriteRule register index.php?mode=register [L]
RewriteRule login index.php?mode=login [L]
RewriteRule reminder index.php?mode=reminder[L]
RewriteRule error index.php?mode=error [L]


Something like that. I'd need to check all my files and rename them in case they are named as the rules.. but I don't want to do that.. So I was wondering if there's a better way to implement this rewrite rule so that it only matches mysite.com/login and not every file or directory that is named login or login.(ext).

Thanks

jdMorgan

4:01 am on Jul 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should really quit coding and start looking up things now. Otherwise, you're playing with fire. With one small typo or logic error in your server config code (and that's what this is), you can seriously damage your search engine rankings for a seriously-long period of time. Being in too much of a hurry at this point could kill your revenue for the next nine months -- although that's the worst I've heard of so far...

Look up the [L] flag in the Apache mod_rewrite documentation, and also take a look at the very-short regular-expressions pattern-matching info in that document. Then correctly anchor the patterns in your rules. Links to these and other resources are cited in our Apache Forum Charter.

Also, URLs ending with slashes imply directories, while those without trailing slashes imply files. So, your example does not involve "directories" -- either actual or virtual. That is, example.com/login would be expected to be a "file" kind of thing, not a directory.

Jim

g1smd

3:09 pm on Jul 19, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The pattern
login
matches any and all requests that contain "login".

You need to look at a Regular Expressions tutorial and the Mod_Rewrite documentation to find out how to restrict the pattern to matching exactly
login
, no more, no less.