homepage Welcome to WebmasterWorld Guest from 54.204.215.209
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
RewriteRule keeps returning "page not found"
keenox




msg:4231001
 3:00 pm on Nov 16, 2010 (gmt 0)

Hello!

I'm pretty new to Apache mod_rewrite. I am working on a site hosted on godaddy.com and I'm pretty sure the problem is because of them. I have the following .htaccess file:


Options +FollowSymlinks

RewriteEngine on

RewriteBase /

RewriteRule ^show$show.php [R]


It keeps redirecting me to the "Page/file not found" page. I tried everything, with/without RewriteBase, using (show),(?=show)(.*),(?<=show)(.*), using a slash before the target, it just doesn't work if the file "show" doesn't exist in the root directory. Once I create a "show" file, it works like a charm.

The "show.php" file is 100% there, so that's not the problem.

The interesting thing is that I tried this file on my free web hosting account and it works with no problems. Any suggestions/solutions?

 

sublime1




msg:4231022
 3:52 pm on Nov 16, 2010 (gmt 0)

Hi --

This RewriteRule is incorrect as written. There needs to be at least one space between the $ after the match pattern and the target path.

Further, I believe you'll need to have the target path start with at least a /, but the strong recommendation from the group here is to fully qualify the redirect URL with domain name. But before you do that, read on.

Is it really the case that you want your pretty urls like "show" to be displayed to the user as ugly urls having the .php tacked on? That's what the "R" flag does: it instructs the browser to re-request the URL with the new path specified. The URL in the address bar will change.

More likely, your objective is to do an internal rewrite: accept a URL in one form from the browser and return a (hidden) internal resource. If this is what you want to do, then don't use the R flag. You should probably use the L flag (meaning: stop processing more rewrite rules).

So I think you code would work like this:


Options +FollowSymlinks
RewriteEngine on
RewriteBase /

RewriteRule ^show$ /show.php [L]


Needless to say, this is just a single case, and unless you have a site with only one page things will get more complicated. If you really need to use Apache rewrite rules for this, cozy up and grow to love and respect [httpd.apache.org...] and the format documentation at [httpd.apache.org...]

And check out the library here (linked near the forum name) of accrued wisdom, most answered by our fearless leader, jdMorgan, not to mention g1smd.

Tom

keenox




msg:4231181
 8:46 pm on Nov 16, 2010 (gmt 0)

@sublime1: Well, I'm not really that new. the space was a typo and i couldn't edit my post.
I've already written that I also tried the slash before the target.
I know what [R] does. I just want to be sure the redirection is correct.

LE: guess i didn't notice the edit button on the left :P

sublime1




msg:4231219
 10:54 pm on Nov 16, 2010 (gmt 0)

Whether the external HTTP 302 redirection resulting from the R flag is correct depends on what you want. The R flag by default causes a temporary redirect (302) not a permanent one (301, which you can do as [R=301];

My guess from the context of your question is that it is not what you want, and that you would probably want to do an internal rewrite which is entirely different.

An external redirect (which can be done many ways, including RewriteRule) tells the user agent (browser, google, etc) to replace the URL that it was given with a new one supplied in the response, then re-issue the request. An internal rewrite maps an external URL to an internal resource.

A typical use for an internal rewrite is to map a "pretty" or "seo friendly" url to an ugly one, or to a specific file. I assume this is what you want.

Tom

keenox




msg:4231221
 11:09 pm on Nov 16, 2010 (gmt 0)

i've discovered the problem or at least i think so. i also posted this on godaddy forums. this is my last post from there:


After intensive testing, I've come to the conclusion that this only happens when there is a file in the root that has a name that matches the regex and followed by .(anything), but not if it's name matches exactly the regex.
E.g. for my case
If there is a file named "show" in the root, the redirect/rewrite works fine.
If there is a file named "show.anything", the redirect/rewrite stops working and instead redirects me to the "Page not found" page.
If there are no files named either "show" or "show.(anything)" the redirects/rewrites work fine.

Another example:

RewriteRule ^abcd$ something

If there is a file named "abcd" in the root, the redirect/rewrite works fine.
If there is a file named "abcd.anything", the redirect/rewrite stops working and instead redirects me to the "Page not found" page.
If there are no files named either "abcd" or "abcd.(anything)" the redirects/rewrites work fine.


The exact same thing happens for Redirect and RedirectMatch.
It seems to me there is a problem in the way your server handles file names/extensions and it should be fixed.


Indeed, I want internal rewrites, but as I said, I used the [R] directive to test if the rewrite works fine (i.e. to be able to see it in the address bar).

This is the .htaccess that I'm currently using and it works fine, but the problem discovered by me still exists:


Options +FollowSymlinks

RewriteEngine on

RewriteBase /

RewriteCond %{HTTP_HOST} !^www\.skyslip\.com

RewriteRule ^(.*)$ http://www.skyslip.com/$1 [R=permanent,L]

RewriteRule ^inbox$show.php?page=messages_show [L]

RewriteRule ^upload$show.php?page=upload_pictures [L]

RewriteRule ^profile$show.php?page=user_show [L]

RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ^(.+[^/])$ $1/

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^/*(.+)/$ /get_user.php?user=$1 [L]

jdMorgan




msg:4231663
 10:23 pm on Nov 17, 2010 (gmt 0)

> when there is a file in the root that has a name that matches the regex and followed by .(anything)

Disable MulitViews (using the Options directive) and turn off AcceptPathInfo if you are not using them.

I strongly suggest that you start all substitution filepaths with a leading slash unless it causes problems on your server. Failure to do so and allowing the client to control the 'root path' leaves your server open to a known exploit. IOW, use "/$1" and never use just "$1".

Also, when posting here, please *do* preview your posts to correct any obvious problems before posting; Such things as the missing spaces in your code above distract from real problems, waste contributors' time and effort, and detract from the discussion of the actual problem. Because mod_rewrite is totally unforgiving, characters which render incorrectly in the forum are also likely to cause problems in parsing and/or processing your actual code. So confrom exactly to the very-specific syntax requirements shown the mod_rewrite docs in order to avoid problems.

Jim

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved