Search replace query string with htaccess - php

I have my site url like this.
https://example.com/page/1/?orderby=low_to_high&s=demo+post&post_type=post&twdg_wsac=1
I want to replace some part of this url using htaccess so the final output should be like this
https://example.com/page/1/?orderby=low_to_high&s=demo+post&post_type=post&custom_posts=1
So I have made changes in the htaccess file like this
RewriteEngine on
RewriteRule ^&twdg_wsac$ &custom_posts
But its not working. Also as you can see in the url there is twdg_wsac=1. So the last "1" is a pagination for post so that would change dynamically as per posts count.
So can someone tell me how to do this?
Any help and suggestions would be really helpful. Thanks
Update
All the parameters in the url are dynamically generated I mean those are the filter parameters. So they cant be same except twdg_wsac

Try with below, we are doing rewrite on query string.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^orderby=(.+)&s=(.+)&post_type=(.+)&twdg_wsac=([\d]+)$
RewriteRule ^ %{REQUEST_URI}?orderby=%1&s=%2&post_type=%3&custom_posts=%4

RewriteEngine On
RewriteCond %{QUERY_STRING} twdg_wsac=([\d]+)$
RewriteRule ^ %{REQUEST_URI}?orderby=low_to_high&s=demo+post&post_type=post&custom_posts=%1 [L]

Related

How to Make .htaccess search by allow mod_rewrite as query string and page number?

My original file is a PHP page named search.php.
Normally, the search function will be:
mydomain.com/search.php cid=&type=search&q=keyword+text&page=2
However, is it possible to rewrite it to mydomain.com/all.html?q=keyword+text&page=[1-anypage]?
I tried to put this code into .htaccess:
RewriteRule ^all.html?q=([^.*]+)&p=([0-9]+)$ search.php?cid=&type=search&q=$1&p=$2 [L]
but something went wrong.
Please help me find the solution. Thanks.
Yes its possible but you can't match against query string in pattern of a RewriteRule, you will need to use RewriteCond directive to check %{QUERY_STRING} something like the following
RewriteEngine On
RewriteCond %{QUERY_STRING} ^q=([^&]+)&page=(.+)$ [NC]
RewriteRule ^all\.html$ /search.php?cid=&type=search&q=%1page=%2 [L]

Rewrite URL to hide query string

I'm trying to mask the query string of my pages in order to hide it's unique page ID. This is important as each ID needs to be unique to a user.
Currently the URL structure looks like this:
http://domain.com/page.php?Page_ID=1234
(where 1234 is any number)
but I need it to look like this:
http://domain.com/page.php
I have tried adding the following to the .htaccess file but it does not seem to make any difference:
RewriteEngine on
RewriteBase /
RewriteRule ^page.php?Page_ID=([0-9]+)/$ page.php [L,QSA,NC]
I've looked at other posts like this one and others, but can't seem to find a solution. Is there something I might be missing here?
Query string is not part of match in rewrite rule, you need to match against %{THE_REQUEST} using a rewriteCond
RewriteEngine on
RewriteCond %{THE_REQUEST} /page\.php\?page_ID=.+ [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R]

modify URL using .htaccess

I want to change url for example http://www.mysite.com/cgshop/admin/index.php?route=common/home will become http://www.mysite.com/cgshop/cp/index.php?route=common/home.
Please help me to figure out this. Thanks in advance.
This should get you what you want.
It tests to see if the URI begins with /cgshop/admin/ and then captures the rest so it can redirect with a 301 to the new url. The QSA means it will also carry over the query string (everything after the ?).
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/cgshop/admin/.*$
RewriteRule ^cgshop/admin/(.*)$ /cgshop/cp/$1 [R=301,L,QSA]
RewriteEngine On
RewriteRule ^cgshop/cp/.*$ cgshop/admin/$1
Try this.......

Want .htaccess to display / insteade of ? in URL and also i am able to get data like $_GET["param1"]

My Actual URL is below.
http://localhost/waterpump/index.php?param1=4498&param2=930&param3=876&param4=201&param5=vis
But my client want in below format.
http://localhost/waterpump/param1/4498/param2/930/param3/876/param4/201/param5/vis
And also i am able to get data using $_GET["param1"]
How can I do this through .htaccess?
Try examples from htaccess tricks
e.g.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^blog/([0-9]+)-([a-z]+) http://corz.org/blog/index.php?archive=$1-$2 [NC]
I am not sure if the number of parameters if fixed or variable but you would do essentially a matching part, where you match the url separated by slashes and then you would "rewrite" it to the original url
If you are set on using $_GET to get the parameters and there will always be between one and five parameters then you could use the following rewrite:
RewriteEngine On
RewriteRule ^waterpump/param1/(.*)/param2/(.*)/param3/(.*)/param4/(.*)/param5/(.*)/ /index.php?param1=$1&param2=$2&param3=$3&param4=$4&param5=$5 [nc]
RewriteRule ^waterpump/param1/(.*)/param2/(.*)/param3/(.*)/param4/(.*)/ /index.php?param1=$1&param2=$2&param3=$3&param4=$4 [nc]
RewriteRule ^waterpump/param1/(.*)/param2/(.*)/param3/(.*)/ /index.php?param1=$1&param2=$2&param3=$3
RewriteRule ^waterpump/param1/(.*)/param2/(.*)/ /index.php?param1=$1&param2=$2 [nc]
RewriteRule ^waterpump/param1/(.*)/ /index.php?param1=$1 [nc]
Editted
If you have implemented this correctly doing a print_r($_GET); on /waterpump/param1/x/param2/y/param3/z/ Should give you something like:
array (
'param1'=>x,
'param2'=>y,
'param3'=>z,
);
There are cleaner ways of doing this that would involve less rewrites, but some changes to your PHP. Also the method above will only work for waterpumps. Will there be anything other than water pumps?

using mod_rewrite to get rid of question mark

alright... so I have this css bundler, using the following link:
http://www.example.com/min/?b=wp-content/themes/mytheme&f=style.css,boxes.css,mods.css,scripts.css
Parameter b is the css folder url and parameter f is the css files to get.
However, to help out my cache, I want the question mark gone. If possible, something like:
http://www.example.com/min/b=wp-content/themes/mytheme&f=style.css,boxes.css,mods.css,scripts.css
I tried the following:
RewriteEngine On
RewriteRule ^/([^/\.]+)/?$ index.php?b=$1 [L]
Needless to say, It did not work.
Your rules are going to loop, you can try adding a condition in front of your rule so that it looks like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^/([^/\.]+)/?$ index.php?b=$1 [L]
But it seems, based on your example URLs, you want something more like this:
RewriteRule ^min/b=(.*)$ index.php?b=$1 [L]
This looks like an unusual thing to do where you want to get rid of ? from query string but want to keep ampersand i.e. &.
Anyway if that's what you really want then you can put this code in your .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteRule ^min/b=([^;]*);f=(.*)$ min/?b=$1&f=$2 [L]
This rule will internally redirect http://www.example.com/min/b=wp-content/themes/mytheme;f=style.css,boxes.css,mods.css,scripts.css to http://www.example.com/min/?b=wp-content/themes/mytheme&f=style.css,boxes.css,mods.css,scripts.css

Categories