Rewriting URL to Query String - .htaccess

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]

Related

Redirect a URL in .htaccess, but keep the query string

I want to redirect a URL in .htaccess, but want to keep the (dynamic) parameters from the query string at the end of the URL (e.g. ?id=1660, ?id=1661, etc.)
E.g.
https://mywebsite.example/service/viewinvoice.php?id=1660
I want to redirect it to:
https://mywebsite.example/whmcs-bridge/?ccce=viewinvoice.php?id=1660
So basically: https://mywebsite.example/service/viewinvoice.php?id=... needs to be redirected to https://mywebsite.example/whmcs-bridge/?ccce=viewinvoice.php?id=...
I tried this below, without any success
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)/service/viewinvoice.php?id= [NC]
RewriteRule ^/?$ /whmcs-bridge/?ccce=viewinvoice.php [L,R=301]
I think this is not the right solution.
Does someone has suggestions?
You need to use the QSA (Query String Append) flag on your rewrite rule. From the documentation:
When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined.
From your example URLs, you don't need to match the query string in a rewrite condition. You are matching the URL path which is done as the first part of the rewrite rule itself.
Your rule should be:
RewriteEngine On
RewriteRule ^/?service/viewinvoice\.php$ /whmcs-bridge/?ccce=viewinvoice.php [L,R=301,QSA]

How can I split URL with htaccess by "?"

For example:
ex.com/read?id=1
should open
ex.com/route.php?action=read&id=1
but url won`t changing.
Try this :
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)\?id=(.+)\sHTTP.*$
RewriteRule ^(.*)$ /route.php?action=$1&id=%2 [QSD,R=301,L,NE]
RewriteCond %{QUERY_STRING} ^action=(.*)&id=(.*)$
RewriteRule ^ /%1?id=%2 [QSD,L,NE]
First I match request that contains query string id=whatever then redirect it with new query string.
Then I match new URI which contains new query string action=whatever&id=whatever and redirect it internally to same original path.
QSD flag is is available in Apache version 2.4.0 and later https://httpd.apache.org/docs/trunk/rewrite/flags.html , I added to discard previous query string and prevent it to be appended.
Clear browser cache then test these rules .
UPDATE:
As per your comment , you want to open /route.php?action=read&id=1 internally while /read?id=1 in browser so , you could do what starkeen answered , but with specific query string and general URI it should look like this :
RewriteEngine on
RewriteCond %{QUERY_STRING} ^id=(.*)$
RewriteRule ^(.*)$ /route.php?action=$1 [QSA,L]
So if query string is existing and strat with id=whatever , /abc?id=123 will get internally from /route.php?action=abc&id=123 and the previous query string will be appending with new one
You can use the following RewriteRule in your /.htaccess :
RewriteEngine on
RewriteRule ^read/?$ /route.php?action=root [QSA,L]
This will rewrite /read?id=1 to /route.php?action=read&id=1 . You do not read to write &id=1 in the Rule's destination as QSA automatically adds it to the Rewrite destination.

Mod Rewrite Appending Query String Parameters, Not Replacing

I am trying to generate an external redirect that involves a few specific tasks.
If specific query string value is found in the URL, redirect.
If redirected, replace one of the query string parameter names but not its value.
If #1 is false, then ignore rewrite and proceed
Example: I have the url http://foobar.com/?a=123&b=456&c=blah
First, if parameter c = blah, redirect to http://barfoo.com/
Second, replace a with x parameter so the final URL is http://barfoo.com/?x=123&b=456&c=blah
Below is my best guess so far after researching on http://mod-rewrite-cheatsheet.com/ and Hidden features of mod_rewrite
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.foobar\.com$ [NC]
RewriteCond %{QUERY_STRING} ^a=(.*)&b=(.*)&c=blah$ [NC]
RewriteRule ^(.*)$ http://barfoo.com/?x=%1&b=%2&c=blah [NC,L,QSA,R=301]
However, the URL is appending the query string, not replacing.
I get redirected to http://barfoo.com/?x=123&b=456&c=blah&a=123&b=456&c=blah
smacks forehead
Removing QSA from the flags solved the issue. QSA means "append the existing query string to the current rewrite rule." It ignores whatever new query string parameters you are adding.
I thought it was needed to have query string parameters exist for the rewrite rule itself.
RewriteRule ^(.*)$ http://barfoo.com/?x=%1&b=%2&c=blah [NC,L,R=301]

htaccess redirection to new params

i want an html redirection to a different php and params, the phps are inside a subdirectory , for example, from an /subdir/index.php?thread=23-post=12 /subdir/to showthread.php?topic=23&topic=12. But isn't working this:
RewriteRule ^/test/index\.php?thread=(.*)-post=(.*)$ /test/showthread.php?topic=$1&topic=$2 [R]
Any suggestion?
thanks in advance
You can't match against the query string in a RewriteRule. You need to match against the %{QUERY_STRING} var in a RewriteCond and use the % backreference:
RewriteCond %{QUERY_STRING} ^thread=([^-]+)-post=(.*)$
RewriteRule ^/?test/index\.php$ /test/showthread.php?topic=%1&topic=%2 [L,R]
Note that your rule maps 2 things to the topic query string param.

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
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
)

Resources