2

I'm trying to setup .htaccess for my website. I want to redirect user to index.php if is entered wrong url address. But when I enter wrong url It does not redirect me to index.php but it shows me an error(Not Found 404 error was encountered while trying to use an ErrorDocument).

My .htaccess file

Options -Indexes
DirectoryIndex index.php

<IFModule mod_rewrite.c>
RewriteEngine On
RewriteBase /rop/
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([A-Za-z0-9-\/&_]+)/?$ index.php?etc=$1 [L]
</IfModule>

<Files config.php>
deny from all
</Files>

ErrorDocument 404 /index.php
ErrorDocument 403 /rop/pages/error/403.html

Thanks for help.

2 Answers 2

1

Do you use localhost? This is work for me.

ErrorDocument 404 http://localhost/[your folder name]/index.php
Sign up to request clarification or add additional context in comments.

Comments

0

It is due to your rewrite rule and ErrorDocument directives that are stepping over each other.

Your rewrite rule is this:

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([A-Za-z0-9-\/&_]+)/?$ index.php?etc=$1 [L]

Which essentially means:

If request is not for a file/directory (not found) then forward to index.php?etc=<uri>.

Now you also have ErrorDocument directive like this:

 ErrorDocument 404 /index.php

If something is not found then forward to /index.php.

PS: Your regex is also incorrect in rewrite rule.

3 Comments

Thank you ... but can you help me to fix it ?
I need to know your requirements first. Why are you rewriting to /index.php?etc= and using ErrorDocument at the same time? Provide some details.
I want to create pretty seo url's .. such as "domain.com/user/profile" .. then I parse URL segments (PHP). When user enters wrong url "domain.com/user/profile/asdad!)_asda/" then I want to redirect him to index.php .. For other errors I would use error Documents (403.html, 500.html and so on..).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.