Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Enable human readable URL's in .htaccess

Preface: Yes this question seems like duplicated, and I found related questions, but answers from there didnt help to me. :(

Hello, I want to add human readable URL's support for my PHP project. For now my URL quesry string looks like:

index.php?url=main/index

I would like to make it looks like:

index.php/main/index

I read following articles:

Stackoverflow

Cheatsheet

Stackoverflow

Stackoverflow

But when I do this:

var_dump($_GET['url']); // get empty array

, get empty array like no url parameter added.

My current .htaccess:

DirectoryIndex index.php

Options +FollowSymLinks

RewriteEngine On

RewriteBase /
RewriteRule ^(.*)$ index.php?url=$1 [NC]

Can somebody help me please? Thanks!

like image 401
nowiko Avatar asked Oct 10 '15 20:10

nowiko


1 Answers

URL: http://domain.com/index.php/controller/action

Rewritten URL: http://domain.com/index.php?url=controller/action

.htaccess

DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine On
RewriteBase /

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

Explanation:

The .* in the pattern ^index.php/(.*)$ matches everything after index.php/ on the incoming URL. The parentheses helps to capture the part as variable $1, which is then added at the end of the substitution URL /index.php?url= + $1.

[L, QSA]: L ignore other rewrite rules, if this fits. QSA means query string append.

like image 159
Jens A. Koch Avatar answered Sep 28 '22 20:09

Jens A. Koch