htaccess RewriteRule keeps query string - .htaccess

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

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
)

Related

Query string in htaccess rule doesn't work

I've a problem with the rule below:
RewriteCond %{QUERY_STRING} ^p=([0-9]+)
RewriteRule ^[ROOT]/([0-9]+)_(.*)$ https://www.[DOMAIN].[EXT]/[ROOT]/$1-$2?page=([0-9]+) [R=301,L]
I need to redirect the old qs parameter (p) to the new one (page), but the result is this:
https://www.[DOMAIN].[EXT]/[ROOT]/[ID]-[STRING]?page=([0-9]+)
I mean: if I've ?p=4 I need to go to ?page=4
I don't understand what's wrong
Thank you all

Rewriting URL to Query String

I know how to rewrite in the reverse direction and process the Query String, but I need to create one query string parameter based on the tag or category, in lowercase. Therefore, I can user the query string as search parameter in my page:
e.g.
news/tag/Shipping/ ==> http://www.example.com/news/?tag=shipping
news/category/Other/ ==> http://www.example.com/news/?category=other
Tried
RewriteEngine On
RewriteRule ^news/([^/]*)$ /?tag=$1 [L]
Thank you.
You need 2 rewrite rules.
RewriteEngine On
RewriteRule ^news/([\w-]+)/?$ news/?tag=$1 [L,QSA,NC]
RewriteRule ^news/([\w-]+)/([\w-]+)/?$ news/?$1=$2 [L,QSA,NC]

.htaccess rewrite rule to change path to query string? [duplicate]

Ok, im pretty new at this and I would really appreciate some help, thanks!
How can i rewrite this in .htaccess correctly?
So I have a query string in my url:
/?url=contact
All i want to do is remove the query string
/contact
Help? I scoured google and I'm learning the syntax right now, but the fact remains..I dont know how to do it just yet. Thanks to all
This was my solution:
RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
Try this:
RewriteEngine On
RewriteRule ^(.*)$ /index.php?url=$1 [L]
To a user on your site, they will see and navigate to this:
http://example.com/contact
But the real page would be something like this:
http://example.com/index.php?url=contact
This bit, [L], tells the server that this is the last line of the rewrite rule and to stop.
RewriteCond %{QUERY_STRING} url=(.*)
RewriteRule index.html %1
(or whatever if it's not index.html, index.php, whatever)
You need to capture the query string, which is not looked at by RewriteRule normally, and use the %1 back reference, not $1 as you would in a capture in a RewriteRule
Before: https://example.com/index.php?user=robert
RewriteEngine On
RewriteRule ^user/([^/]+)?$ index.php?user=$1 [L,QSA]
After: https://example.com/user/robert

.htaccess rewrite URL with a question mark "?"

My aim is this URL:
component/users/?view=registration
To:
registration.html
the .htaccess is in the folder mysite of the website.
i tried this:
RewriteBase /mysite
RewriteRule ^component/users/?view=registration$ registration.html$ [R=301,L]
But i doesnt work...
When i try this:
RewriteRule ^component/users/_view=registration$ registration.html$ [R=301,L]
it works very well.
So how can i fix this problem with the question mark. I already read thats it is not a part of the URL (its appended). I have read that i have to use something like querystring, but i didn't really understand the syntax.
Maybe someone could write the solution of this problem? Would be awesome =)
You need to use %{QUERY_STRING} to capture the query string data:
RewriteCond %{QUERY_STRING} ^view=(.*)$
RewriteRule ^component/users/?$ %1.html? [R=301,L]
The above rule/condition will take the value of the query string view and use it to form your redirect if the path component/users matches.
RewriteRule ^component/users/\?view=registration$ registration.html$ [R=301,L]
You need a \ because ? is part of a regular expression. To Use it as a string u need to escape it with a \
Linktip: http://ole.michelsen.dk/tools/regex.html

htaccess rewrite querystring and remove empty value

first, sorry for my bad English.
I try to rewrite url generated from Form Get and redirect that.
my url is like this:
http://www.mysite.com/properties?action=search&agreement=for-rent&category=my-category&type=&zone=my-zone&city=my-city
and I have this .htaccess configured:
11. RewriteCond %{QUERY_STRING} ^action=(?:[a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)$
12. RewriteRule (.*) %{REQUEST_URI}/%1/%2/%3/%4/%5/? [R=301,L]
So basically all my request are direct to index.php.
21. RewriteCond %{REQUEST_URI} !index\.php|resources|hidden
22. RewriteRule ^(.*)$ index.php/$1 [L]
All works, but the problem is when I have an empty value in query string, the rule add double slash and the above url (for example whit &type=&zone=my-zone... type have empty value) will translate like that:
http://www.mysite.com/for-rent/my-category//my-zone/my-city/
The question is: How can i remove in .htaccess the double slash generated if i have one or more empty value in query string?
Thanks
Easiest is to do another redirect (not real pretty as it requires two 301's).
RewriteCond %{THE_REQUEST} //
RewriteRule .* $0 [R=301,L]
The fun part is that when the url is loaded with a double slash in it, mod_rewrite will automatically remove this. So as you can see above you'll just have to rewrite the url to itself, kind of.

Resources