htaccess for load subdomain with parameters - php

I would like to know how I can get a second parameter when I load data from an htaccess, I have everything working so that I get the first parameter that is the subdomain, the second parameter would be the "GET" that it gets from the URL, for example:
I want to load:
subdomain.domain.com/category?idx=22
With the following code I get the first parameter that is the subdomain but I can't get the idx after category
This is the code:
RewriteCond %{HTTP_HOST} !^(www\.)?domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain.com$ [NC]
RewriteCond "%{REQUEST_URI} /%1" !^(/[^/]+)[^\s]*\s\1$
RewriteRule ^category$ loader-category.php?&id=%1&idx=$2 [L]
what's the correct regex for this?
I appreciate any help

RewriteRule ^category$ loader-category.php?&id=%1&idx=$2 [L]
What would $2 be referring to here? You don’t have any capturing groups in the pattern.
Use the QSA flag here, so that the original query string gets appended / merged with the new one you are creating.
https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_qsa
RewriteRule ^category$ loader-category.php?id=%1 [L,QSA]

Related

Redirect rule to php query but remove any query string in the requested url

My url is being redirected using .htaccess as follows:
RewriteRule ^b/([^/]+)/([^/]+)? b/view.php?id=$2&name=$1
Friendly url -> translates to php url
domain.com/b/hello/2 -> b/view.php?id=2&name=hello
BUT when someone comes to the site as follows:
domain.com/b/hello/2?query=xyz
I don't know how to get rid of the ?query=xyz
I have tried everything including [QSD] and I can't seem to get it to work.
Update
I have managed to get it to work with the following but it does two 301 redirects instead of one:
RewriteCond %{THE_REQUEST} \?[^\ ]+
RewriteRule ^b/(.*)$ /x/$1? [R=301,L]
RewriteRule ^b/([^/]+)/([^/]+)/([^/]+)?$ b/view.php?id=$2&&name=$1
Check if adding the question mark at the end of the rule will cancel appending query string from left side
RewriteRule ^b/([^/]+)/([^/]+)? b/view.php?id=$2&name=$1? [QSD,L]
You can use an additional Rule to catch for GET parameters and strip them off.
RewriteCond %{QUERY_STRING} .+
RewriteRule ^(.*)$ /$1? [R=301,L]
RewriteRule ^b/([^/]+)/([^/]+)? b/view.php?id=$2&name=$1
To make it only work on the /b/ subfolder, use this:
RewriteCond %{QUERY_STRING} .+
RewriteRule ^b/(.*)$ b/$1? [R=301,L]
RewriteRule ^b/([^/]+)/([^/]+)? b/view.php?id=$2&name=$1
The first rule will redirect everything that matches your rule to the URL without any GET parameters (note the ? at the end of the rewrite rule, it will strip off the parameters).
The second rule will match in the case the first rule cannot be applied, i.e., when there are no paramaters

PHP - URL with multiple parameters and same name causing unexpected behaviour

I have a Rewrite Rule as
RewriteRule settings/([a-zA-Z0-9_]+)/?$ settings/?path=$1 [NC,L,QSA]
It basically takes path parameter and rewrite url like this
http://localhost/settings/?path=abc => http://localhost/settings/abc
But there's a problem! when I provide something like http://localhost/settings/abc/?path=xyz the path parameter is overwritten with xyz and it opens http://localhost/settings/abc/ with data of xyz
After further digging I noticed that the problem is caused by the parameters in URL itself. By default its picking up the value of last parameter with same name http://localhost/settings/account/?path=profile&path=account&path=profile
so the value that i can get in this case for $_REQUEST['path'] is profile. How can I just pickup the first value from the same parameters and ignore the rest of them?
One thing I can do is to remove [QSA] from the RewriteRule, that will do the fix but I need other parameters passed in url as well.
How can I achieve this?
PHP recognizes brackets [] appended to a parameter, e.g.
http://localhost/settings/account/?path[]=profile&path[]=account&path[]=profile
will be translated into an array
$path = $_GET['path'];
print_r($path);
shows
Array ( [0] => profile [1] => account [2] => profile )
This way, you can access any of the "paths" you like.
I could imagine some RewriteCond surgery, in order to remove part of a query string
RewriteCond %{QUERY_STRING} ^(.*)&path=.*?&(.*)$
RewriteRule ^ %{REQUEST_URI}?%1&%2
RewriteCond %{QUERY_STRING} ^path=.*?&(.*)$
RewriteRule ^ %{REQUEST_URI}?%1
RewriteCond %{QUERY_STRING} ^(.*)&path=.*$
RewriteRule ^ %{REQUEST_URI}?%1
Depending on where the path is, this would remove it from the query string. And finally, the last rule would add the new path again
RewriteRule settings/([a-zA-Z0-9_]+)/?$ settings/?path=$1 [NC,L,QSA]
Although, I don't know, if this is working at all, and how (in)efficient this is.
There is one caveat, you must check for REDIRECT_STATUS to prevent an endless loop.
You can use these 2 rules in your .htaccess:
# remove path= parameter from query string, if it exists
RewriteCond %{THE_REQUEST} \?(.*&)?path=[^&]*(?:&(.*))?$ [NC]
RewriteRule ^settings/. %{REQUEST_URI}?%1%2 [L,R=301,NE,NC]
# your existing rule
RewriteCond %{THE_REQUEST} \?(.*&)?path=[^&]*(?:&(\S*))? [NC]
RewriteRule ^settings/ %{REQUEST_URI}?%1%2 [L,R=302,NE,NC]
RewriteCond %{QUERY_STRING} !(?:^|&)path= [NC]
RewriteRule ^(settings)/(\w*)/?$ $1/?path=$2 [NC,L,QSA]

Mod rewrite issue redirect with post

In fact I am working on a small php script, and now I am struggling with doing redirect using mod-rewrite.
What i want is to redirect
www.xx.com/motivational/api.php?latest=1
to
www.xx.com/api.php?latest=1&app=motivational
I tried this but it doesn't work:
RewriteRule ^/motivational/api\.php$ /api.php?latest=&%{QUERY_STRING}
%{QUERY_STRING} represents the entire query string, in your case latest=1. So when you append it to ...?latest= in your substitution string the result is ...?latest=latest=1 which is not what you want.
Change your rule to
RewriteRule ^/motivational/api\.php$ /api.php?%{QUERY_STRING}&app=motivational
and you should be fine.
Or you could do:
RewriteRule ^/motivational/api\.php$ /api.php?app=motivational [QSA]
The QSA flag means to append the new query string to the old, rather than to replace it, so your latest variable will not be lost.
You can not match against query strings in a RewriteRule, You will need a RewriteCond to match query strings in url :
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^/]+)/api\.php\?latest=1 [NC]
RewriteRule ^ /api.php?latest=1&app=%1 [NC,L,R]
%1 is part of the regex "([^/]+)" in RewriteCond, it contains dynmically captured path in the request line.

Redirect Urls With Parameters Using Htaccess

I did a "site:" command in google to view the indexed urls and found a lot of urls with parameters all pointing to the same page.
Its a static site, but about 5 years ago it was a Wordpress site, which may be how those links were created and indexed by Google. The 3 main links I found are as follows. They all look like these 3 but with different id's.
http://example.com/index.php?option=com_banners&task=click&bid=41
http://example.com/?option=com_content&view=article&id=86&Itemid=201
http://example.com/phlebotomy-jobs?&pid=6774238282444518&q=Phlebotomy&pg=7
My question is how do I redirect these links, with all of the different parameter ids, to their corresponding pages. The first two go to the homepage and the 3rd one goes to a /phlebotomy-jobs page.
So, what I'm looking for is, for example, this url: http://example.com/phlebotomy-jobs?&pid=6774238282444518&q=Phlebotomy&pg=7 to redirect to http://example.com/phlebotomy-jobs
Basically removing everything from the ? on.
Well, not sure if it's the "best" way, but this, for example, works:
RewriteCond %{QUERY_STRING} option=
RewriteRule (.*) /$1? [R=301,L]
RewriteEngine On
RewriteRule ^index.php?$ /path/page.php [R=301,L,QSA]
RewriteRule ^$ /path/page.php [R=301,L,QSA]
RewriteRule ^phlebotomy-jobs?$ /path/page.php [R=301,L,QSA]
The QSA RewriteRule Flag appends the query string.
https://httpd.apache.org/docs/2.4/rewrite/flags.html
Edit:
Since you're not concerned with retaining the query strings:
RewriteEngine On
RewriteCond %{REQUEST_URI} (^/index.php$|^/$|^/phlebotomy-jobs$)
RewriteCond %{QUERY_STRING} .
RewriteRule (.*) /$1? [R=301,L]
Note: This method prevents passing any query strings to either of those two pages.

Redirect with RewriteRule keeping keys redirected to the new url

I got this url:
somesite.org/news.php?a=0&b=20
What I want - redirect it to
somesite.org/specialnews/20
If you see - key a means the type. So a=0 means specialnews. So I need redirect only those urls, that have a=0 and base url (news.php) also to /specialnews/.
What I've tried is:
RewriteCond %{QUERY_STRING} ^a=0&b=(.*)$
RewriteRule ^news\.php\?a\=0&b\=(.*)$ specialnews/$2 [R=301]
didn't help ;)
Need directions.
You can't match query string params in RewriteRule, but you can capture paramters via RewriteCond and use it (with %) in RewriteRule:
RewriteCond %{QUERY_STRING} a=0&b=(.*)
RewriteRule ^news.php /specialnews/%1? [R=301]

Categories