I've got a RewriteRule like this:
RewriteRule ^thesection$ /sections.php [L]
RewriteRule ^thesection/(.*)$ /sections.php?show=$1 [L]
so, if I enter domain.com/thesection
-> it works perfect
but!, if I enter domain.com/thesection/hello
it redirects to domain.com/thesection?show=hello
I don't know what I'm doing wrong and I've spent hours googling. Please help!
Thanks in advance
htaccess rewrite rules can be used to direct requests for one subdirectory to a different location, such as an alternative subdirectory or even the domain root. In this example, requests to http://mydomain.com/folder1/ will be automatically redirected to http://mydomain.com/folder2/.
There are two main directive of this module: RewriteCond & RewriteRule . RewriteRule is used to rewrite the url as the name signifies if all the conditions defined in RewriteCond are matching. One or more RewriteCond can precede a RewriteRule directive.
[L] means "last rule" i.e. stop processing RewriteRules after this point.
RewriteRule specifies the directive. pattern is a regular expression that matches the desired string from the URL, which is what the viewer types in the browser. substitution is the path to the actual URL, i.e. the path of the file Apache servers. flags are optional parameters that can modify how the rule works.
Try this and let me know
RewriteEngine On
RewriteRule ^thesection/?$ /sections.php [L,QSA]
RewriteRule ^thesection/([a-z0-9\-_]+)/?$ /sections.php?show=$1 [L,QSA]
this will redirect domain.com/thesection/hello
to sections.php?show=hello
the QSA flag means:
This flag forces the rewrite engine to append a query string part of the substitution string to the existing string, instead of replacing it. Use this when you want to add more data to the query string via a rewrite rule.
so you can add more parameters eg: http://www.domain.com/thesection/hello/?page=1
this will output:
Array (
[show] => hello
[page] => 1
)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With