Apache htaccess modem redirection using GET controller variables on PHP index page

I work with the MVC PHP user infrastructure, and the index page (acting as a router) receives the GET "do" variable, which contains the path to be routed. If this variable is not set, the default Auth controller is the login method.

require_once('config.php');
$controllerAction = isset($_GET['do'])?$_GET['do']:"auth/login";
require_once('core/main.php');

Then the index page (source code above) transfers this $ controllerAction to the main.php file, which automatically loads the main controller and then loads the requested controller.

Thus, the URIs in this structure are of the form mysite.com/?do=controller/method/variable, and I need it to be in the form mysite.com/controller/method/variable.

Here is the .htaccess file I tried to use, it just didn't work (I have other htaccess files running on the same server, so this is not an Apache problem): (

RewriteEngine On
RewriteRule ^([^/]*)$ /?do=$1 [L]

Someone suggested I do this using PHP, but I'm not sure how to do this.

Edit: The error is that I get the message "This page cannot be displayed", 404 errors when I try to access the mysite.com/controller/method links directly, and not the default mysite.com?do=controller/ method

Further editing

(note that other virtual hosts work fine on my localhost):

(XAMPP) Apache shared hosting information:

<VirtualHost *:80>
    DocumentRoot "D:\sites\mysite.com\root\wwwroot"
    ServerName mysite.com
    ServerAlias mysite.com
  <Directory "D:\sites\mysite.com\root\wwwroot">
        Options Indexes FollowSymLinks Includes ExecCGI
        AllowOverride All
        Order allow,deny
        Allow from all
  </Directory>
</VirtualHost>

File Structure (Windows):

D:\
--sites
----mysite.com
--------@client_details
--------root
-----------@devfiles
-----------@vars_pwd
-----------wwwroot
--------------config
--------------core
--------------application
------------------controllers
------------------libraries
------------------models
------------------views
----------------------css
----------------------javascript
----------------------images
----------------------icons
+3
source share
6 answers

, .htaccess. , . img/ , css , javascript, .. RewriteCond Apache , . . , , QSA (, Query String Append), GET script.

:

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

, , . , , , . - apache.

mod_rewrite, , , . root :

a2enmod rewrite
/etc/init.d/apache2 restart

( Module rewrite already enabled, ), Apache. , .

, VHost .htaccess . , AllowOverride FileInfo ( All). . :

<VirtualHost *:*>
    ServerName test.example.com
    ServerAlias www.test.example.com
    DocumentRoot /home/sites/test/
    <Directory "/home/sites/test/">
        Allow from all
        AllowOverride All
        Options +Indexes
    </Directory>
</VirtualHost>

, Apache, - .

, . /var/log/apache2/error.log (debian). , .

+4

RewriteEngine On
RewriteRule ^([^/]*)$ index.php?do=$1 [L]
+2

Try

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?do=$1 [L]
+2

apache, . , :). , .

+2

MVC,

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?do=$1 [QSA,L]
</IfModule>

, . URL site.com/controller/method

+2

, , .

, index.php, :

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

, . www.example.com/site/, - index.php. (// ).

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

404, :

  • , .htaccess, AllowOverride . , .htaccess . , .htaccess .
  • MVC, , . , , MVC- - 404.

Edit: Just reading your description, I noticed that you said that the URL should basically be something like mysite.com/?do=controller/method/variable. If this format is very strict, then you will also need to introduce rules to remove any leading or trailing slashes, for example. The following rewrite rule should do this:

RewriteRule ^\?(.*)\?$ /index.php?do=$1 [L,QSA]

(This makes the option leading and trailing slashes, but it must remove them from the actual value that you pass in order to do).

+1
source

All Articles