1

I'm building a PHP application with a simple login view. 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?

My app structure is like this:

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

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

index.php
.htaccess
Rafael Tavares
  • 5,678
  • 4
  • 32
  • 48
alexhoma
  • 342
  • 1
  • 7
  • 19
  • Possible duplicate of [How to write htaccess rewrite rule for seo friendly url](http://stackoverflow.com/questions/28168375/how-to-write-htaccess-rewrite-rule-for-seo-friendly-url) – JimL May 12 '16 at 16:34

1 Answers1

0

There are two solution that I know:

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

Create an .htaccess file and put this code to 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 will redirect to index.php and you can get the 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 the 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
Rafael Tavares
  • 5,678
  • 4
  • 32
  • 48
Majid Abbasi
  • 1,531
  • 3
  • 12
  • 22