简体   繁体   中英

MVC remove “index.php” and “GET” request from the url

I'm building a php application with a simple login view for now. I'm routing the view using two GET parameters in my url (controller and action):

http://www.web.com/index.php?controller=access&action=login

The thing is, I don't like this url, and I wanted to show it like this:

http://www.web.com/access/login

I read about some redirects in the htaccess but no one works as expected. Anyone knows the solution or a best practice to do this?

I'll also sow you my app structure if it helps:

    app/
      |-- view/
      |   |-- login.php
      |-- base.php

    src/
      |-- controller/
      |   |-- AccessController.php
      |-- router.php

    index.php
    .htaccess

Thanks for advanced!

there are two solution which i know:

1) redirect all request to main index.php and route pattern on that file.

create and .htaccess file and put this code for redirect all urls to index.php

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule .* index.php [F]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf|raw|xml|jpg|ajx))$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]

in this case all request with redirect to index.php and you can get url from $_SERVER['REDIRECT_URL'] and route requests

If users request /a/b.html, you can $_SERVER['REDIRECT_URL'] and use from different parts, you should use secure code in 2 solution

2) handle routes with .htaccess file directly, but i recommend solution #1

Options +FollowSymLinks
RewriteEngine on

RewriteRule a/b\.html index.php?controller=$1&action=$2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM