Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

htaccess RewriteRule keeps query string

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

like image 246
Marsupial000 Avatar asked Dec 09 '11 10:12

Marsupial000


People also ask

What is RewriteRule in htaccess?

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/.

What is RewriteCond and RewriteRule?

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.

What is RewriteRule?

[L] means "last rule" i.e. stop processing RewriteRules after this point.

What is Apache RewriteRule?

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.


1 Answers

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
)
like image 57
Book Of Zeus Avatar answered Sep 20 '22 12:09

Book Of Zeus