Conflict in Archive . Htacess

Asked

Viewed 23 times

0

On htacess:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php

RewriteRule ^blog/(.*)/$ blog/assuntos.php?disciplina=$1
RewriteRule ^blog/(.*)/(.*)$ blog/pagina.php?disciplina=$1&assunto=$2

The urls:

http://localhost/teste.com/blog/portugues/
http://localhost/teste.com/blog/geografia/mundo

On the pages:

$disciplina=$_GET['disciplina'];
$assunto=$_GET['assunto'];

Problem:

With these rules I can get the second url but not the first, the first is returning php subjects. in the $_GET['assunto'];. I wanted the first to return at $_GET['disciplina'];.

When I remove one of the rules the other works perfectly, but with both gives conflict. Someone knows what may be happening?

1 answer

1


Place [L] at the end of each RewriteRule that are in fact different "rules", as is the case, it is also necessary to note that the regular expression with (.*) will catch anything, is like a "joker" in regex, so you have to tell the regex what he has to disregard, and in case what was missing was disregard the bars /

In short, instead of this (.*) this would be right ([^/]+), the [^/] marries anything that is not slash, the sign + indicates that it is to pick up until finding the next "match", so it should look like this:

RewriteRule ^blog/([^/]+)/$ blog/assuntos.php?disciplina=$1 [L]
RewriteRule ^blog/([^/]+)/([^/]+)$ blog/pagina.php?disciplina=$1&assunto=$2 [L]

To sum up, when I did that ^blog/(.*)/$, he would accept anything, like:

/blog/foo
/blog/foo/bar
/blog/foo/bar/baz
/blog/sobre/etc

And even if the first rule were to be ignored, the second rule would still fail, because it would accept anything as well, including other bars in the middle /, at least two bars within the expression.

Learn the basics of . htaccess for rewrites:

Many people usually live from copying codes around without understanding why, understanding the basic principles will save you a lot of headache

  • Perfect. Only the [L] already worked ,I did not know it. I’ll hit the rest also to be more straight. Thanks!!!

  • @Rod would probably work even with the [L] because in order of interpretation the module itself must prioritize the most approximate rule of the current URL, but the problem is that a user write Urls with several bars (at least theoretically it would marry the regex)

  • @Rod Take the opportunity to learn the basics of . htaccess for rewrites: https://answall.com/q/102722/3635 Many people usually make a living from copying codes around without understanding why, understanding the basic principles will save you a lot of heartache

  • I will give a read, bought book, course, but it is very difficult regex. Right now I’m trying to put an optional /? at the end of each url and will not. Oh sick thing. rs

  • @Rod regex use the basics for now, but learn the mod_rewrite of Apache (.htaccess) which is key to avoiding some problems :)

Browser other questions tagged

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