/    Sign up×
Community /Pin to ProfileBookmark

Apache Rewrite — Why Won’t Work?

I have Apache 2 installed on my system. I have uncommented the load rewrite module line in the httpd.conf file. I have an htaccess file with these simple lines:

[code]RewriteEngine On
RewriteRule /Index$ /index.shtml[/code]

But /Index still returns 404. I don’t get it. What’s missing?

to post a comment
Full-stack Developer

7 Comments(s)

Copy linkTweet thisAlerts:
@ScleppelJul 07.2006 — In .htaccess files RewriteRule patterns don't start with a /:
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^Index$ /index.shtml


If that doesn't work, you might need [url=http://httpd.apache.org/docs/2.0/mod/core.html#allowoverride]AllowOverride[/url] set to All in the [url=http://httpd.apache.org/docs/2.0/mod/core.html#directory]<Directory>[/url] block for your document root.
Copy linkTweet thisAlerts:
@FeelLikeANutauthorJul 07.2006 — Thanks! Taking off that slash seemed to do the trick. Now, however, I can't get deeper directory rewrites to work.RewriteRule ^Examples/NoGraphics$ examples/no-graphics.shtmlThis returned a 404 page. I also tried putting an htaccess file in the [i]examples[/i] directory itself:RewriteRule ^NoGraphics$ no-graphics.shtmlThis didn't work either, however. Any thoughts?
Copy linkTweet thisAlerts:
@ScleppelJul 07.2006 — I don't see any problems with the rules. Check that the file exists and that you're typing it in right. Does the You could try [b]/[/b]examples/... too.

This works fine for me:
Options +FollowSymLinks -MultiViews

RewriteEngine On

RewriteRule ^Index$ /index.shtml [L]
RewriteRule ^Examples/NoGraphics$ /examples/no-graphics.shtml [L]
Copy linkTweet thisAlerts:
@FeelLikeANutauthorJul 07.2006 — I definitately have it in right. I even copy-pased the directory- and file name to make sure there were no typos. I tried with and without the initial slash.

Is there something else I can try? Some other piece of information you can use?

Perhaps this version of Apache has a bug? It is 2.2.2. Perhaps you could download and install that version and see if everything still works the same.
Copy linkTweet thisAlerts:
@ScleppelJul 07.2006 — Right, it seems to be a case problem because of the directory of the same name, but different case.

You can either use a lowercase "e" in the RewriteRule (you can still use an uppercase "e" in the URI)
RewriteRule ^examples/NoGraphics$ /examples/no-graphics.shtml [L]
or the NC (No Case) flag:
RewriteRule ^Examples/NoGraphics$ /examples/no-graphics.shtml [NC,L]

That should work.

Edit: Are you using Windows?
Copy linkTweet thisAlerts:
@FeelLikeANutauthorJul 07.2006 — The no case flag did the trick, though I don't quite understand why it would be necessary -- the URL itself had the same uppercase letters. But whatever, I guess. It works.

And yes, I am on Windows XP.
Copy linkTweet thisAlerts:
@ScleppelJul 07.2006 — It's probably a combination of windows being case insensitive and mod_rewrite working after the file has been determined.

So first Apache is trying to find the real file. It finds the directory /examples and changes the case of it's (internal?) request_uri variable because that's what exists. Then it doesn't find the file (but that doesn't matter, it goes through mod_rewrite anyway) and the request goes to mod_rewrite.

Now when mod_rewrite applies the pattern, the cases don't match:
## - - [07/Jul/2006:22:03:03 +0100] [##][##] (3) [perdir C:/server/Apache2.2/htdocs/] applying pattern '^Examples/NoGraphics$' to uri 'examples/NoGraphics'
and you get a 404. Change the "E" or add the NC flag, and it matches.
×

Success!

Help @FeelLikeANut spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.4,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...