Forum Moderators: phranque

Message Too Old, No Replies

htaccess - getting file to not 404 without trailing slash

         

tec4

7:30 pm on Sep 7, 2011 (gmt 0)

10+ Year Member



Trying to figure out why the page I'm creating works when I place a trailing slash but re-directs to a 404 when i take it off.

For example, this code 404's without trailing slash and works when it's like: example DOT com/item-test-page/
---note: when I take out the stuff relating to the query it works...aka- when i take out the "([A-Za-z0-9\-/+_\(\)]+)" and the "&query=$2"...the query obviously doesn't work but going to the page without the trailing slash works

RewriteRule ^([A-Za-z0-9\-+\(\)#%|_,]+-test-page)([A-Za-z0-9\-/+_\(\)]+)$ /file.php?item=$1&query=$2 [L]

The page/code below, however, works without the trailing slash (just like I'd like the other one to do...the one above). Looks like: example DOT com/zip-properties/55555

RewriteRule ^zip-properties/([A-Za-z0-9\-+\(\)#%|_,]+)/?(\d+)?$ /directory/zip-properties.php?zip=$1&page=$2 [L]

I tried toying with it for quite a while but can't seem to make and progress.

Any thoughts?

tec4

8:16 pm on Sep 7, 2011 (gmt 0)

10+ Year Member



Think i figured it out (at least not going to 404 and didn't screw anything else up that i could see). What does everything think of this instead:

RewriteRule ^([A-Za-z0-9\-+\(\)#%|_,]+-test-page)(/.*)?$ /file.php?item=$1&query=$2 [L]

g1smd

9:13 pm on Sep 7, 2011 (gmt 0)

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



"re-directs to a 404"

Does it actually "redirect" - serve a 30X status code then show the 404 error at that different URL?

lucy24

9:38 pm on Sep 7, 2011 (gmt 0)

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



zip-properties/([A-Za-z0-9\-+\(\)#%|_,]+)/?(\d+)?$


Is this a cut-and-paste from your working htaccess? I'm pretty sure it doesn't say exactly what you wanted it to say. As written:

zip-properties/{buncha stuff, some of which should never occur in an url} {maybe a slash} {maybe some numbers}

What range of possible page titles are you trying to capture here? Always be as specific as possible so you don't risk capturing anything you didn't mean to.

Does it actually "redirect" - serve a 30X status code then show the 404 error at that different URL?

From the user's POV: does the browser's address bar show the address of your 404 page or of the (nonexistent) page you typed in?

tec4

10:55 pm on Sep 13, 2011 (gmt 0)

10+ Year Member



@g1smd - sorry, fairly new. No the url in the browser does not actually get re-directed but does show the 404 page after it was sent...what is the main difference here? I notice that sometimes I encounter an actual re-directed url and sometimes i encounter the file not actually being re-directed?

@lucky24 - yes, this was pretty much copy/paste, other than just changing the name of the file...don't really have much experience with this stuff other than working with pre-existing code (trying to learn though). So, the code I copied and pasted was code written by a developer previously for the site (a real estate site working with addresses, unit numbers and such) so i'm not really sure what exactly the minimum would be for handing those things. It does seem to be accepting a lot of parameters, just not sure what exactly I could get away with instead.

Thanks for the responses, you two.

lucy24

1:19 am on Sep 14, 2011 (gmt 0)

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



A 404 page (or 403) is actually a nice illustration of the difference between a rewrite and redirect.

Try to go to a nonexistent page, and your browser will show the page address but the window will show a 404 page (customized, one hopes). Try to go to a directory that has no index file-- for ordinary humans, the only time you meet a "forbidden"-- and you get the same thing, only with the 403 page. Those are REWRITES: you're seeing the content of one location while you're officially in a different location.

By default, if you don't give instructions to the contrary, your server will interpret anything without an extension as a directory name. If there is no trailing slash it will add one, and will then look for likely files such as index.html or index.php. (The exact list depends on the server, sometimes with help from the browser. You might even end up at main.jsp)

If and only if you have mod_{I forget its name} enabled, the server may also backtrack and look for files whose name is whatever you entered. But you can't rely on this happening.

In short: If you want anything without an extension to take the user to a page other than {blahblah/index.html}, you have to code it yourself.

tec4

7:33 pm on Sep 14, 2011 (gmt 0)

10+ Year Member



Oh, okay. That actually makes quite a bit more sense now.

I really appreciate you elaborating that for me.

Thanks, lucy24.