12

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!

RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
nowiko
  • 2,507
  • 6
  • 38
  • 82

6 Answers6

5

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.

Jens A. Koch
  • 39,862
  • 13
  • 113
  • 141
  • Hello, thanks for repply but actually I want revrse convertion, from `index.php?url=cont/action` to `index.php/cont/action` – nowiko Oct 13 '15 at 20:21
  • or it mean that server will get as second but I can write it like in first case? – nowiko Oct 13 '15 at 20:24
  • Maybe i got the question wrong. Not sure. This will rewrite the first URL `http://domain.com/index.php/controller/action` to `http://domain.com/index.php?url=controller/action` - and `$_GET['url']` will contain the string `controller/action`. – Jens A. Koch Oct 13 '15 at 20:30
  • yes, you got write, but it doesnt work, $_GET aray still empty. – nowiko Oct 13 '15 at 20:41
  • I replace my htaccess file with that yoy are sent, but it doesnt work, maybe I need add some extra configuration? – nowiko Oct 13 '15 at 20:43
  • Hmm, whats the output of `var_dump($_SERVER['REQUEST_URI']);`? – Jens A. Koch Oct 13 '15 at 20:45
  • it outputs `'/project/index.php/test/about'` – nowiko Oct 13 '15 at 20:51
  • The full request_uri doesn't contain a `url=` part and so `$_GET['url']` is not set at all. (For better debugging of the rewrite rule, you could raise the Apache log level to see the url rewriting events in the log. Maybe they are not triggered.) – Jens A. Koch Oct 13 '15 at 21:03
  • Hello, I enable this line in apache2.conf but in `/var/log/apache2/error.log` I cant seee any errors related to mode rewrite, and `tail -f error_log|fgrep '[rewrite:'` from apache2 docs return info about no found files – nowiko Oct 15 '15 at 16:14
  • Maybe add the relevant part of your apache error log to the question. Is it possible that the index.php file is in a subfolder `projects`? If so, please adjust the substitution url, e.g. `/projects/index.php?url=$1`. – Jens A. Koch Oct 15 '15 at 16:32
  • 1
    @excluded if REQUEST_URI output '/project/index.php/test/about' try to change the htaccess from RewriteRule ^index.php/(.*)$ /index.php?url=$1 [L,QSA] to RewriteRule ^/project/index.php/(.*)$ index.php?url=$1 [L,QSA] – Thesee Oct 17 '15 at 19:57
  • @JensA.Koch I think that the real issue is that the OP doesn't understand what he is doing. Your answer is right, but I guess the OP is still using `index.php?url=controller/action` in his code instead of `index.php/controller/action`. – GuyT Oct 20 '15 at 09:05
2

You have

index.php?url=main/index

Yuo want to rewrite (and then user redirect) to this

index.php/main/index

What about trying this?

DirectoryIndex index.php

Options +FollowSymLinks

RewriteEngine On

RewriteBase /

RewriteCond %{QUERY_STRING} ^url=([a-z]+)/([a-z]+)$
RewriteRule ^.*$ http://mydomain.site/index.php/%1/%2 [R=302,L]

R=301 or 302 depend on your need

On this example i assumed that on the original url there are only a-z chars. Just to clarify (due the fact generally the process is inverse) on this way the users will be redirect, this rules does not convert links on your page.

Thesee
  • 161
  • 5
  • hello, thanks for reply but this doesnt help to me. I do this stuff in htaccess maybe I should move to virtual host configuration? – nowiko Oct 18 '15 at 14:13
  • i have a doubt, what type of link do the users on site see? – Thesee Oct 18 '15 at 21:15
1

In line
RewriteRule ^(.*)$ index.php?url=$1 [NC] the (.*) matches the part of the url up to '?' where the query begins.
To use the values passed in the query you need the QSA flag. Means Query String Append.

Greg Kelesidis
  • 1,069
  • 14
  • 20
  • Hi, thanks for answer, now line looks like: `RewriteRule ^(.*)$ index.php?url=$1 [NC, QSA]`, but still have same result – nowiko Oct 10 '15 at 21:13
0

enter image description hereTry the below code, Only considered for index.php you can replace it as per your requirement. Let me know if you need further assistance.

DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine ON
RewriteBase /
RewriteRule ^(index\.php)/([0-9a-zA-Z\/_-]{1,99}) index.php?url=$2

What worked for me is:- <?php var_dump($_GET); ?> this is the only thing I did on url http://localhost/baba.php/abcd . and original .htaccess file

DirectoryIndex baba.php RewriteEngine ON RewriteBase / RewriteRule ^(baba\.php)*/([0-9a-zA-Z\/_-]{1,99}) baba.php?url=$2

Prafulla Kumar Sahu
  • 9,321
  • 11
  • 68
  • 105
0

If Your URL : www.abcd.com/index.php/abcd/...

.htaccess

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Gausul
  • 265
  • 3
  • 16
0

For Magento 2.4 Magento will always drop any GET parameters from the URL in a htaccess rewrite. The only way I found to make it work was to create a rewrite module. The setup is here: https://magento.stackexchange.com/a/158811/109113

Doodled
  • 186
  • 2
  • 11