0

I'm trying to redirect all the user's requests to my index page, except the one made to the root folder.

So www.domain.com/page.ext is fine while www.domain.com/folder/page.ext has to be redirected.

Here's my .htaccess:

RewriteEngine on
RewriteRule ^([^/]+)/?$ /index.html [L]
ErrorDocument 404 /index.html

It causes an Internal Server Error and I don't know why.

Basically I want to force the user to access just the index.html page in every kind of situation.

Suggestions?

EDIT:

Just to give some more infos, here's my site structure:

/
|-- index.html
|-- css
   |-- site.css
|-- js
   |-- site.js
|-- images
   |-- img1.png
   |-- img2.png
   |-- img3.png

As you can see it's really simple.

What I need to do is to show always my index.html (which uses css/site.css, js/site.js and the images are used in the css)

So it doesn't matter what page the user requests, my site must show the index one. This means that the index has to be shown even in case of 404 error and even if the user requests a page in a folder which doesn't exists!

1 Answer 1

3

Within an .htaccess file, the relative path to the directory (or the RewriteBase, if one is configured) is used for both the match string (you've got that right in your regex, no leading slash) and the replace string.

Try RewriteRule ^([^/]+)/?$ index.html [L]

Edit:

To avoid doing this for your image/js/css files, you can use RewriteCond to prevent the rule from being applied:

RewriteCond %{REQUEST_URI} !^/images [NC]
RewriteCond %{REQUEST_URI} !^/css [NC]
RewriteCond %{REQUEST_URI} !^/js [NC]
RewriteRule ^([^/]+)/?$ index.html [L]
6
  • I've tried it. www.site.com/folder is redirected, www.site.com/folder/page.ext seems redirected too but it won't load index's css so the page is not shown as it should (the page is ok in the first case)
    – StepTNT
    Apr 7, 2013 at 12:25
  • @StepTNT Right, you're rewriting all requests, including those for the CSS and other resources like images and javascript. Can you clarify what requests you'd like to rewrite, and which ones you don't want to? For instance, maybe only rewrite the request if there isn't a static file that would be served? Apr 7, 2013 at 18:55
  • Sorry for being not so clear. What I need is tho show ALWAYS the index page, no matter what's the requested one. So any request to any kind of url should end on www.site.com/index.html
    – StepTNT
    Apr 8, 2013 at 9:57
  • @StepTNT Right. The problem with that is than an image, a CSS file, or a javascript file are all individual HTTP requests. To the server, they're no different to a request for the HTML page that the user put in their address bar. So, to develop a rule that only targets those HTML pages, you need to help me understand what those requests that we don't want to redirect look like. Is all your static content in a different directory, like /static or /images and /css? Or should we just look at the file extensions - which file extensions do you use in there? Apr 8, 2013 at 15:40
  • Thank you for your help, I really appreciate your patience. I've updated my questions, hoping that now you have all the needed infos.
    – StepTNT
    Apr 9, 2013 at 13:35

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .