Forum Moderators: phranque

Message Too Old, No Replies

Rewrite with endslash

trying to get my files to look like a dir

         

woden

5:45 pm on Aug 22, 2005 (gmt 0)

10+ Year Member



Hey,

I've been trying to get this rewrite rule to do as I want, but it doesn't like to cooperate with me. :(

I'm trying to get my .ASP pages to "look" like this:
[myproject.tld...]

but I also need it to work if you just write:
[myproject.tld...]

Right now I'm using this:
RewriteCond %{REQUEST_FILENAME}.asp -f
RewriteRule ^(.*)$ $1.asp [L]

This works, but only without the endslash, it just removes the .asp.

I've also tried with:
RewriteCond %{REQUEST_FILENAME}.asp -f
RewriteRule ^(.*)/?$ $1.asp [L]

and:
RewriteCond %{REQUEST_FILENAME}.asp -f
RewriteRule ^(.*?)/?$ $1.asp [L]

but I just keep getting a 500 error, and the errorlog says maxredirects has been reached.

Thanks in advance for any help

jdMorgan

6:46 pm on Aug 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Check the meaning of "-f" -- you'll probably be happier with:

RewriteCond %{REQUEST_FILENAME}.asp [b]!-f[/b]
RewriteRule ^(.*)/?$ $1.asp [L]

Jim

woden

7:20 pm on Aug 22, 2005 (gmt 0)

10+ Year Member



Thanks for the reply Jim. I checked out the -f now.

But this still does not work, when I tried your rule it gave me a 404. Could this be because of the "!" in front of the "-f"? Does it think that the input string is actually a dir, and does not find it?

If I remove the "!", this works:
[something.tld...]

but not:
[something.tld...]

jdMorgan

8:57 pm on Aug 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, I don't know what exactly you are trying to do, or what your diferctory/file structure looks like, so I'm not sure whether you want to check for "file exists" or "file does not exist."

But let's examine what the code I posted actually should do:


# IF the requested resource does NOT exist as a file with a .asp extension
RewriteCond %{REQUEST_FILENAME}.asp !-f
# THEN rewrite any request (with optional trailing slash) to same URL-path with trailing ".asp"
RewriteRule ^(.*)/?$ $1.asp [L]

Maybe what you want is this:

# IF the requested resource does NOT exist as a directory
RewriteCond %{REQUEST_FILENAME} !-d
# THEN rewrite any request (with optional trailing slash) to same URL-path with trailing ".asp"
RewriteRule ^(.*)/?$ $1.asp [L]

Examine your server error and access logs. These will often be very helpful in identifying the problem.

Jim

woden

9:57 pm on Aug 22, 2005 (gmt 0)

10+ Year Member



I'm sorry, I'm notorious for being vague. I'm very new to mod_rewrite, or any programming really, so I'm not surprised if I'm not making much sense. But I will try.

What I want this rewrite rule to do is:

IF the requested resource exists with a .asp extension
THEN rewrite any request (with optional trailing slash) to same URL-path without trailing ".asp"

At least I think that explains it. If my logic "skills" above failed, I'll try to explain it better like so:

If I write [mydomain.tld...] (or without the last slash) I want to get a file named something.asp, tho of course, I want it to display in the adressbar like: [mydomain.tld...] (or without the slash)
The requested file is a .asp, I just want it to "look" like they are all directories.

Do you follow me, or I'm not making any sense?

I have a bunch of other rules that rewrites the forum queries et cetera, they all look like this, but with filename variations (they work fine with or without a trailing slash):

RewriteCond %{REQUEST_URI} ^/thread([0-9]+)/?$
RewriteRule ^ /forum_posts.asp?%1 [L]

(I don't think at least) they depend on the first rule, that I'm having problems with:

RewriteCond %{REQUEST_FILENAME}.asp -f
RewriteRule ^(.*)$ $1.asp [L]

EDIT: This is the error from the log, it's always the same:
[Mon Aug 22 23:24:45 2005] [error] [client XXX.XXX.XXX.XXX] mod_rewrite: maximum number of internal redirects reached. Assuming configuration error. Use 'RewriteOptions MaxRedirects' to increase the limit if neccessary.

jdMorgan

10:39 pm on Aug 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



As an experiment, try this:

RewriteCond %{REQUEST_URI} !\.asp$
RewriteCond %{REQUEST_FILENAME}.asp -f
RewriteRule ^(.*)$ $1.asp [L]

The added RewriteCond prevents a request from being rewritten if it already ends with ".asp". Although this should not be necessary to prevent a rewrite loop, it may give us some information.

Jim

woden

11:18 pm on Aug 22, 2005 (gmt 0)

10+ Year Member



Getting closer I think. :)

That rule works with:
[mydomain.tld...]

but when I try:
[mydomain.tld...]

I get a 404, telling me it didn't find /something/.asp on the server.

Here is the log:
[Tue Aug 23 01:12:46 2005] [error] [client XXX.XXX.XXX.XXX] File does not exist: /dir/dir/dir/dir/mydomain.tld/something/.asp

jdMorgan

12:34 am on Aug 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In that case, the following should work:

RewriteCond %{REQUEST_URI} !\.asp$
RewriteCond %{REQUEST_FILENAME}.asp -f
RewriteRule [b]^(.*)/?$[/b] $1.asp [L]

but I still don't know why the added RewriteCond is necessary, because it implies that after a first rewrite to /example.asp, that when the rule runs again (which it will in .htaccess) that the server successfully finds the REQUEST_FILENAME "example.asp.asp" which it most definitely should not.

There are common problems that make RewriteConds testing REQUEST_FILENAME fail when they should match, but I don't know of any problems that would make them match when they should fail... Strange.

Jim

woden

10:58 am on Aug 23, 2005 (gmt 0)

10+ Year Member



It works without the slash, but with the slash it delivers a 404, "The requested URL /something/.asp was not found."

If you know a way of doing this without the added cond, I'm all ears. It's just there cos we couldn't get it to work without it. And my knowledge in this is quite limited.

jdMorgan

12:39 pm on Aug 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



With the addition of the optional slash to the RewriteRule pattern, the result you report is "impossible." Based on your error message, the RewriteRule appears to be active, but it's not removing that slash, which it must do. So that leaves one more possibility, and that is that you have a cached copy of that "page" in your browser.

Try flushing your browser cache (Delete all Temporary Internet Files) after any change to your code, and test again.

Jim

woden

1:02 pm on Aug 23, 2005 (gmt 0)

10+ Year Member



Hmm, I "flushed" all the temp files, re-uploaded the htaccess and I've tried in a couple other browsers to be sure. The result is the same, it says it can't find /something/.asp

The errorlog says:
[Tue Aug 23 14:58:17 2005] [error] [client XXX.XXX.XXX.XXX] File does not exist: /dir/dir/dir/dir/mydomain.tld/something/.asp

jdMorgan

2:40 pm on Aug 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



OK, one more thing to try:

RewriteCond %{REQUEST_URI} !\.asp$
RewriteCond %{REQUEST_FILENAME}.asp -f
RewriteRule ^([^/]+)/?$ $1.asp [L]

This replaces the greedy ".*" pattern with a pattern that matches anything except a slash. It is possible that the ".*" is "consuming" the trailing slash, including it in the $1 back-reference, and leaving the "/?" subpattern to "starve." The ".*" pattern, although easy to use, often causes side-effects, because it is greedy (it matches as much as possible) and it will match anything. For this reason, it should be avoided when more-specific patterns can be used.

Jim

woden

4:41 pm on Aug 23, 2005 (gmt 0)

10+ Year Member



Ah, wonderful. It works!

Thank you very much, jdMorgan! :)