Htaccess - Redirect all requests to a single file every time

Asked

Viewed 437 times

0

I am trying to use a. htaccess file to redirect all requests to a single file, but I am facing some problems.

My . htaccess is like this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

My index.php file is in the same directory as the . htaccess file, and I also have in this same directory a 1.php page file. My problem is when I access:

localhost/page1.php

When I specify the file extension, it is processed, rather than redirecting to index.php

How could you make even with the extension the request redirect to the index.php file, without affecting CSS and JS imports?

  • 1

    Hint: The line RewriteCond %{REQUEST_FILENAME} !-f serves to verify if the file exists, if the condition is true, executes it.

1 answer

1


It’s a half-assed solution, you can find better by searching on google (And yes you have, you should search before posting question here on Stack guy :/ )

Use the following code in your htaccess

NOTE: If you don’t have much knowledge in . htaccess and are already using one, backup it, create a new one and paste the following into it:

RewriteEngine on
RewriteCond $1 !^(index\.php|style\.css|js\.js)
RewriteRule ^(.*)$ /index.php/$1 [L]

Where the RewriteCond $1 !^(index\.php|style\.css|js\.js) are the files that should not be subject to the redirect rule, being style.css (.ext file) | < separate from files ie arq\.ext|arq2\.ext etc.....

I hope it helps :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.