.htaccess - rewriting non-user-friendly URL

Asked

Viewed 215 times

2

When I make the requisition :

http://localhost/project/mycontroller/

the URL rewrites to

http://localhost/project/mycontroller/?l=mycontroller

That is, beyond the /mycontroller o . htaccess (I suppose) is concatenating in the url the same parameters of GET only in the unfriendly way.

Note 1 : l is my variable from $_GET same and everything works perfectly, even if I mess with the non-user URL.

Note 2 : At first I thought it was a redirect, but I put one exit() in the first line of the index and even then the URL continued rewriting the non-user part.

How to solve this?

.htaccess

<Files magic>
ForceType application/x-httpd-php5
</Files>

<IfModule mod_rewrite.c>
RewriteEngine On 
Header append Vary User-Agent       

#?l=local -> /local
RewriteRule ^([a-z0-9_]+)$ ?l=$1 [NC,L]
RewriteRule ^([a-z0-9_]+)/$ ?l=$1 [NC,L]

#?l=local&sl=sublocal -> local/sublocal
RewriteRule ^([a-z0-9_]+)/([a-z0-9_]+)$ ?l=$1&sl=$2 [NC,L]
RewriteRule ^([a-z0-9_]+)/([a-z0-9_]+)/$ ?l=$1&sl=$2 [NC,L]

#?l=local&sl=sublocal&cod=1 -> local/sublocal-1
RewriteRule ^([a-z0-9_]+)/([a-z0-9_]+)/([0-9]+)$ ?l=$1&sl=$2&cod=$3 [NC,L]
RewriteRule ^([a-z0-9_]+)/([a-z0-9_]+)/([0-9]+)/$ ?l=$1&sl=$2&cod=$3 [NC,L]

#?l=local&sl=sublocal&cod=1 -> local/sublocal/var
RewriteRule ^([a-z0-9_]+)/([a-z0-9_]+)/([a-z0-9]+)$ ?l=$1&sl=$2&var=$3 [NC,L]
RewriteRule ^([a-z0-9_]+)/([a-z0-9_]+)/([a-z0-9]+)/$ ?l=$1&sl=$2&var=$3 [NC,L]

</IfModule>
  • 1

    Welcome to Stack Overflow! I suggest you read the topic How to ask a good question... At the moment it is not possible to understand what your doubt is, I suggest you edit the question and make it clearer what you want to do and what you are not getting

  • Yeah, I traded "someone already solved" for "how to solve," but Rodrigo’s right. Please, edit the question to clarify what the exact problem is.

  • Whoa, guys! Thanks, I’ll try to explain better.. hehe

1 answer

0


More generally, since it is not known what you want or how you will use your . htaccess, to get the query string "l", it would be enough:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?l=$1

By calling the address indicated in the question, you would receive "mycontroller/" stored in $_GET['l']. This way, you can also pass something like:

seu_dominio/project/mycontroller/segundo_parametro/terceiro_parametro/

what would return:

mycontroller/segundo_parametro/terceiro_parametro/

Simply treat the string to separate the data. For example, in PHP:

$qs = explode("/", $_GET['l']);

that would be:

Array (
    [0] => my_controller
    [1] => segundo_parametro
    [2] => terceiro_parametro
    [3] => 
)
  • These two Rewritecond directives were missing, and today I was thinking about stopping making specific rules for a general ! Your comment fell like a glove ! Thanks @Blau !

  • this one. htaccess worked for everything going straight to/index.php, but when I call a controller from an existing page it fails, send me to the folder’s file system, imagine what it might be?

  • @rcBytes, it will be a pleasure to try to help, but we can’t do that in the comments. Feel free to open a new question detailing the problem. So more opinions can come, with a great possibility to cover more cases of use of . htaccess rules.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.