Htaccess rewrite query string to another query string - apache

Trying to make redirects:
domain.com/search/?q=query&p=2
domain.com/niche/?q=weddings&p=2
domain.com/tag/?q=rings&p=2
to
domain.com/index.php?search=query&page=2
domain.com/index.php?group_name=weddings&page=2
domain.com/index.php?tag=rings&page=2
without changing url.
Using this rules:
RewriteCond %{QUERY_STRING} ^q=([^/]+)&p=([0-9]+)$
RewriteRule ^search/$ index.php?search=%1&page=%2 [L]
RewriteCond %{QUERY_STRING} ^q=([^/]+)&p=([0-9]+)$
RewriteRule ^niches/$ index.php?group_name=%1&page=%2 [L]
RewriteCond %{QUERY_STRING} ^q=([^/]+)&p=([0-9]+)$
RewriteRule ^tag/$ index.php?tag=%1&page=%2 [L]
Search and Niches are working good, and Tag is giving 404. However, sraight url (like domain.com/index.php?tag=rings&page=2) is working great. Can you please help to figure out? Thanks!

Related

Change Query String using HTACCESS?

How do I change the query from
https://www.example.com/page.php?foo=bar
to
https://www.example.com/page.php?foo=something
just using htaccess ?
I had tried :
RewriteCond %{QUERY_STRING} foo\=bar$ [QSR]
RewriteRule page\.php\?foo\=bar$ page\.php\?foo\=baz [L,R=301]
not not working, testing it in the online htaccess tester -- https://htaccess.mwl.be/
You can use:
RewriteCond %{QUERY_STRING} foo=bar$
RewriteRule ^page\.php$ page.php?foo=baz [L,R=301]
Because the querystring is not part of the RewriteRule test.

Remove part of the query string with mod_rewrite

I am not very good with .htaccess at all, so I want to achieve something very simple, but I can't. What I want to do is to redirect certain files to test.php, and if test is ok, PHP redirects back to original page. It works fine, I add the "test=ok" part to the original URL, that way I don't get a redirect loop. However, I want to remove the test=ok query part from the original URL on redirection. How can I achieve that???
TL/DR
I have several URLs I want rewritten through mod_rewrite.
examples:
http://example.com/?time=1&test=ok
http://example.com/?test=ok
How can I remove the &test=ok and the ?test=ok parts using .htaccess?
Right now I have:
RewriteCond %{QUERY_STRING} ^test=ok$ [NC]
RewriteRule (.*) /$1? [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} (/[^.]*|\.(php|html?|js))$ [NC]
RewriteCond %{QUERY_STRING} !test=ok [NC]
RewriteRule .* test.php [L]
But that doesn't remove the test=ok part... :(

URL re-write not working without www

I am having some issues with URL re-writing that I cannot figure out. Here's the problem.
This URL IS NOT redirecting properly:
http://domain.com/index.php?en=oldpage
HOWEVER, this URL IS redirecting properly:
http://www.domain.com/index.php?en=oldpage
The only difference in the url that is not redirecting properly is the absence of the www.
Here is the re-write I am using:
RewriteCond %{QUERY_STRING} ^en=oldpage
RewriteRule ^(index.php/|)$ /newpage.html? [R=301,L]
I also have this re-write BEFORE other re-writes, to handle url's without "www.":
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
Why will it not re-direct without the "www." ? Any help would be much appreciated. Thank you.
I guess there is some code/rule below those rules adding query string back to your URLs.
Try these rules instead based on THE_REQUEST variable which doesn't get overwritten:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{THE_REQUEST} \s/+(?:index\.php)?\?en=oldpage [NC]
RewriteRule ^ /newpage.html? [R=301,L]
Make sure to test it after clearing your browser cache.

Rewrite URL with subdirectory and file

i am trying to use .htaccess to rewrite the URLs for my site, here is what i would like
currently, the urls look like this
http://www.mysite.biz/store/cart.php?m=new_arrivals
All of the sites are loaded using php GET and the only thing that ever changes is the query string after cart.php so i would like to rewrite to....
http://www.mysite.biz/cart.php?m=new_arrivals
I have been searching all over the internet and have tried out various techniques to do this and none of them have worked. i've tried this...
RewriteRule ^store/(.*)$ http://www.mysite.biz/$1 [R=301,L]
this...
RewriteCond %{REQUEST_URI} ^/store($|/)
RewriteRule ^.*$ /store/cart.php [L]
and this...
RewriteCond $1 !^store
RewriteRule ^(.*) /store/$1 [L]
ive been developing web for a while now but this is my first time really doing anything with .htaccess and i am totally stumped. any help will be greatly appreciated
Try this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+store/(cart\.php[^\s]+) [NC]
RewriteRule ^ /%1 [R=302,L]
RewriteRule ^(cart\.php)$ /store/$1 [L,NC]

Redirect Subdomain to new domain

Hi guys trying to get a 301 redirect working and having trouble. I need to redirect sub.domain1.com to www.domain2.com and make sure that any file names or parameters get sent over with it.
This is what I was trying:
RewriteCond %{HTTP_HOST} ^domain1.com [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [L,R=301]
I also tried this:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^sub\.domain1\.com$ /www.domain2.com? [R=301,NE,NC,L]
Where am I messing up?
You missed the subdomain part and proper escaping.
RewriteCond %{HTTP_HOST} ^sub\.domain1\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [L,R=301]
Further explain can be found in this question.
Rule of thumb for rewriterules: from the most complex to the less complex.
And don't forget the QSA directive (QSA = Query String Append = "make sure that any file names or parameters get sent over with it")
RewriteCond %{HTTP_HOST} ^sub\.domain1\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [QSA,R=301,L]
Tell me if it works.