Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htaccess to create centralize php files

I have no idea how to achieve the following.

Currently I have a problem. Whenever a user goes to www.example.com or any link with same domain it will run a centralize.php (example). This PHP script will determine which PHP to call based on the links, something like centralized php files.

I tried to use .htaccess to achieve this without success. What am I doing wrong or is there are another way to do this?

RewriteEngine  on
RewriteRule /(.*) /iindex4.php$1 [PT]

The above is what I've tried so far but it does't meet my requirement and is obviously flawed or buggy.

Question summary - navigate any url with same domain it will run a certain PHP file - is there another way to do this beside .htaccess

like image 651
Leon Avatar asked Feb 15 '26 23:02

Leon


2 Answers

Try this:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

Then you can retrieve the requested URI from the script through the $_GET['q'] variable.

like image 101
Alessandro Desantis Avatar answered Feb 18 '26 13:02

Alessandro Desantis


.htaccess is the way to do this, and this approach is commonly used, for instance by mediawiki, wordpress, and frameworks like Kohana.

To redirect everything to index.php:

RewriteEngine On
RewriteRule .* index.php/$0 [PT]

To allow existing files to be executed (which is not what you want, I guess):

RewriteEngine On

# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]
like image 21
GolezTrol Avatar answered Feb 18 '26 11:02

GolezTrol



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!