Enable human readable URL's in .htaccess
Asked Answered
S

6

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!

Strachan answered 10/10, 2015 at 20:5 Comment(2)
Does the URL still rewrite but doesn't pass through the url in $_GET?Boiney
Is your htaccess in root folder or in a subfolder ? Also, you'll have to add a condition to avoid a loopLittell
B
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.

Blakemore answered 13/10, 2015 at 20:17 Comment(12)
Hello, thanks for repply but actually I want revrse convertion, from index.php?url=cont/action to index.php/cont/actionStrachan
or it mean that server will get as second but I can write it like in first case?Strachan
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.Blakemore
yes, you got write, but it doesnt work, $_GET aray still empty.Strachan
I replace my htaccess file with that yoy are sent, but it doesnt work, maybe I need add some extra configuration?Strachan
Hmm, whats the output of var_dump($_SERVER['REQUEST_URI']);?Blakemore
it outputs '/project/index.php/test/about'Strachan
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.)Blakemore
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 filesStrachan
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.Blakemore
@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]Lawannalawbreaker
@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.Fireboard
L
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.

Lawannalawbreaker answered 17/10, 2015 at 19:48 Comment(2)
hello, thanks for reply but this doesnt help to me. I do this stuff in htaccess maybe I should move to virtual host configuration?Strachan
i have a doubt, what type of link do the users on site see?Lawannalawbreaker
I
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.

Industry answered 10/10, 2015 at 21:6 Comment(1)
Hi, thanks for answer, now line looks like: RewriteRule ^(.*)$ index.php?url=$1 [NC, QSA], but still have same resultStrachan
C
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

Chorus answered 19/10, 2015 at 16:18 Comment(3)
I did on my localhost, and It worked I tested it for baba.php and changed it to index.php for you, ok,I will try again and modify my answer.Chorus
damn, still not working, could you please screen with results? maybe I really doing something wrong...Strachan
@excluded have you tried the updated one or solved let us know.Chorus
D
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]
Deadhead answered 19/10, 2015 at 18:18 Comment(0)
M
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

Mcneese answered 17/1, 2023 at 10:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.