Wordpress Won't Keep My Url Params - php

I'm working on WP website and anytime I add url params to the url, it redirects the page to a version without the params.
Example:
http://mysite.com/?foo=bar -> redirects to -> http://mysite.com/
http://mysite.com/contact-us/?foo=bar -> redirects to http://mysite.com/contact-us/
How can I fix this? We need certain params to load with the page for various reasons.
Contents of .htaccess (edited to add QSA - which isn't working):
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L,QSA]
</IfModule>
# END WordPress

You have to use query_vars to make that happen. WordPress stores all the query string parameters that it may need in a global object and it ignores everything else.
You need to instruct it to do the following:
Instruct WordPress to save your variables. You add a filter to query_vars to do that. An example is given in the link below.
Retrieve your data using $wp_query->query_vars['customvariable'] instead of the regular _GET or _POST.
The details can be found here: http://codex.wordpress.org/Custom_Queries#Custom_Archives

Add the "query string append" (QSA) flag to the end of your rewrite rules.
'qsappend|QSA' (query string append) 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.
RewriteRule . /index.php [L,QSA]
# Without QSA: http://mysite.com/contact-us/?foo=bar →
# http://mysite.com/index.php?page_name=contact-us
# With QSA: http://mysite.com/contact-us/?foo=bar →
# http://mysite.com/index.php?page_name=contact-us&foo=bar
See the Apache documentation for more information.

Related

htaccess multiple query string not working

I want to get this URL:
example.com/scooter-details/1/vespa-sprint
But the URL I get is:
example.com/scooter-details.php?scooter_id=1&scooter_brand=vespasprint&scooter_model=
The scooter_model "sprint" is in the scooter_brand query, Normally it has to be scooter_brand=vespa&scooter_model=sprint. Hope you can help me with this
Here is the htaccess code
Options +FollowSymLinks -MultiViews
RewriteEngine On
# SEO FRIENDLY URL
# Redirect "/scooter-details.php?service_id=<num>" to "/scooter-details/<num>"
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^scooter_id=(\d+)&scooter_brand=([^/.]+)&scooter_model=([^/.]+)$
RewriteRule ^(scooter-details)\.php$ /$1/%1/$2%2-$3%3? [QSD,L,R=301,N]
# Rewrite "/scooter-details/<num>" back to "scooter-details.php?service_id=<num>"
RewriteRule ^(scooter-details)/(\d+)/([^/]+)$ $1.php?scooter_id=$2&scooter_brand=$3&scooter_model=$4 [L]
You most probably need the NE (noescape) flag on the RewriteRule directive if you are capturing elements of the query string (which is already URL-encoded) and using these to build the URL-path. Otherwise you will end up doubly-URL-encoding parts of the resulting URL-path - which is what this looks like.
%2520 is a doubly URL-encoded space. ie. you would seem to have %20 (ie. a space) in the query string you are capturing from.
However, the rule you have posted does not seem to relate to the example URLs given?

.htaccess redirect /subpage/ to /subpage?/wp-login

Trying to use .htaccess rule to do the wp-login JS check on first visit by appending ?/wp-login to the url since it's interferring with Sucuri firewall when using password protection.
I've created a test subdomain to try to get the htaccess redirect to work before using it on the live site:
RewriteCond %{QUERY_STRING} ^protectedpage$
RewriteRule ^(.*)$ https://testing.no11.ee/protectedpage?/wp-login [R=302,L]
view here: testing.no11.ee/protectedpage
Unfortunately this does not add the query arg to the url. What am I doing wrong here?
Expected result when visiting page should be https://testing.no11.ee/protectedpage?/wp-login as the browser url.
Full htaccess:
# BEGIN WordPress
# The directives (lines) between "BEGIN WordPress" and "END WordPress" are
# dynamically generated, and should only be modified via WordPress filters.
# Any changes to the directives between these markers will be overwritten.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{QUERY_STRING} ^protectedpage$
RewriteRule ^(.*)$ https://testing.no11.ee/protectedpage?/wp-login [R=302,L]
</IfModule>
# END WordPress
RewriteCond %{QUERY_STRING} ^protectedpage$
RewriteRule ^(.*)$ https://testing.no11.ee/protectedpage?/wp-login [R=302,L]
This checks that the QUERY_STRING is set to protectedpage, but in your example it's the URL-path that is /protectedpage, not the query string.
You also need to first check that the query string is not already set to /wp-login, otherwise you'll get a redirect loop.
However, you've also put the code in the wrong place. Note the WordPress comment that precedes the code block - you should not manually edit this code. This directive also needs to go before the WordPress front-controller, otherwise, it's simply never going to get processed.
Try the following instead before the # BEGIN WordPress comment marker:
RewriteCond %{QUERY_STRING} !^/wp-login$
RewriteRule ^(protectedpage)/?$ /$1/?/wp-login [R=302,L]
This matches an optional trailing slash on the requested URL, but it redirects to include the trailing slash in the target URL.
(You do not need to repeat the RewriteEngine on directive.)
No need to include the scheme + hostname if you are redirecting to the same. The $1 backreference simply saves repetition and refers to the matched URL-path, ie. protectedpage (without the trailing slash) in this example.
However, this always redirects and appends /wp-login to this URL - not just the "first visit" - is that really what you require? Otherwise, you need to somehow differentiate between "first" and "subsequent" visits (by detecting a cookie perhaps?)
UPDATE: Minor addition: how would one improve this to add ?/wp-login to all urls that have the page /subpage/ as parent i.e /subpage/page-1 and /subpage/page-2 would result in /subpage/page-1?/wp-login etc? I tried using (.*) but this delets the subpage from the url...
You could do something like the following:
RewriteCond %{QUERY_STRING} !^/wp-login$
RewriteRule ^(subpage/[^/]+)/?$ /$1/?/wp-login [R=302,L]
The [^/]+ subpattern matches any character except / - so only the second path segment, excluding the optional trailing slash. This is similar to .*, but this would capture everything, including any trailing slash, so would result in a double slash in the redirected URL.

.htaccess 301 and PHP query string

I'm wondering if the wonderful world of the SO community can help me with this one.
I have the following URL's that I would like to redirect/rewrite in my .htaccess file.
1. Redirect
I am trying to 301 redirect this URL:
http://example.com/staff-view.php?i=ACCOUNT_ID_EXAMPLE
to
http://example.com/staff-view/ACCOUNT_ID_EXAMPLE/
I have attempted the following:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+staff-view\.php\?i=([^\s]+) [NC]
RewriteRule ^ /staff-view/%1/? [R=301,L]
however when navigating to the from URL it does not redirect.
2. PHP Query String
I am using the below to rewrite:
RewriteRule ^staff-view/([^/]+)/?$ /staff-view.php?i=$1 [L,QSA,NC]
and accessing through URL
http://example.com/staff-view/ACCOUNT_ID_EXAMPLE/
it correctly directs me to the right page, but when attempting to access ACCOUNT_ID_EXAMPLE via the following methods:
<?
var_dump($_REQUEST);
var_dump($_GET);
?>
They both are empty:
array(0) { } array(0) { }
I would appreciate any help, if you need any more info, please let me know.
Update 1
Updated .htaccess file:
RewriteEngine On
RewriteRule ^staff/([^/]+)/?$ /staff-view.php?i=$1 [L,QSA,NC]
RewriteBase "/"
RewriteCond %{REQUEST_URI} ^staff-view\.php$
RewriteCond %{QUERY_STRING} ^i=([A-Z0-9_]*)$
RewriteRule ^(.*)$ ^staff/%1/
I have attempted to access the file from:
http://example.com/path/to/site/staff/ACCOUNT_ID
and it DOES NOT work. However, if I access the file from:
http://example2.com/staff/ACCOUNT_ID
it WORKS.
But if I go to http://example2.com/staff-view.php?i=ACCOUNT_ID it does not redirect to http://example2.com/staff/ACCOUNT_ID - this is not the end of the world, but I would like to fix it, but the deep directory issue as a priority :).
Try using this:
RewriteEngine On
RewriteRule ^staff-view/([^/]*)$ /staff-view.php?i=$1 [L]
Trying to parse %{THE_REQUEST} (e.g., "GET /staff-view.php?i=account_id HTTP/1.1") to extract both the path and the query string could work but it's unnecessarily complex. I think it's much simpler to use two rewrite conditions that take advantage of the server variables %{REQUEST_URI} and %{QUERY_STRING} because they are pre-populated with the info you're interested in.
Try the following:
RewriteEngine On
#If you don't have this set in your htaccess,
# Apache may prepend the final path with your on disk dir structure
RewriteBase "/"
#Rewrite if the /staff-view.php page is requested
RewriteCond %{REQUEST_URI} ^/staff-view\.php$
#Rewrite if the query string contains i parameter anywhere. Assumes
# ID can be only digits; include all allowed chars. eg: [a-zA-Z0-9_]
#Don't forget the '&' before {{QUERY_STRING}} otherwise the match
# will fail if i is the first parameter
RewriteCond &%{QUERY_STRING} &i=([0-9]+)
#Rewrite the account ID as part of the path. Append the query string
# in order to preserve other query parameters (eg: if user asked for
# /staff-view.php?i=123&x=boo, you want to preserve x=boo. a 301
# redirect tells the browser to go to the new path and to remember it
#This will stop processing and cause the browser to make a new request
RewriteRule ^(.*)$ /staff-view/%1/ [QSA,R=301,L]
#Finally, we want to silently forward any request matching the last
# redirect target to the actual file that will serve the request.
# The account ID should be of the same format as above: ([0-9]+). The
# [L] flag tells the server to stop looking for new instructions
RewriteRule ^staff-view/([0-9]+)/$ /final.php?i=$1 [QSA,L]
The logic is easy to follow: If the path requested is /staff-view.php, and if the query string contains the i parameter, tell the user's browser to go instead to /staff-view/ID, preserving the other query params. Finally, when the browser asks for this new path, silently (without telling the browser) forward the request to final.php along with the ID and other query params

How do I turn PHP string into slug in Wordpress

I have a page in wordpress I am using with a slug called Bad-Debt-Recovery-in/. I am using a custom php query on that page with strings in the URL's like this
Bad-Debt-Recovery-in/?zipcode=55555&location=Chambers%20County+AL
How can I make this url into a slug like this
Bad-Debt-Recovery-in/55555/Chambers-County/AL/
as a rewrite? Any help would be appreciated!
UPDATE:
This code is actually what I am using. I also made it simpler and created a third variable named "state". One rewrite is for City and one is for County page:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^Bad-Debt-Recovery-And-Collection-Agencies-Services-In\/([^\/]+)\/([^\/]+)\/([^\/]+)\/? Bad-Debt-Recovery-And-Collection-Agencies-Services-In/?zipcode=$1&city=$2&state=$3 [QSA,L,NC]
RewriteRule ^Bad-Debt-Recovery-And-Collection-Agency-Services-In\/([^\/]+)\/([^\/]+)\/([^\/]+)\/? Bad-Debt-Recovery-And-Collection-Agency-Services-In/?countyid=$1&county=$2&state=$3 [QSA,L,NC]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php
</IfModule>
# END WordPress
What you are asking for, and seeking to accomplish is called: Converting a request path into a query string.
Use .htaccess RewriteRule directive to modify the incoming request
RewriteRule ^Bad-Debt-Recovery-in\/([^\/]+)\/([^\/]+)\/([^\/]+)\/? Bad-Debt-Recovery-in/?a=$1&b=$2&c=$3 [QSA,L,NC]
Each ([^\/]+) captures path elements into a variables $1,$2,$3...
The ? at the end simply denotes that the last / is optional or else the last match could fail
the \ simply escape the '/' for literal interpretation
Add as many ([^\/]+) as needed and capture them in the query
zipcode=$1&location=$2+$3
The modifiers at the end [QSA,L,NC] are called flags
QSA appends the query string to the end of the rewrite if any exist
L simply says this is the last rewrite
NC means not case sensitive
This is your final solution:
RewriteRule ^Bad-Debt-Recovery-in\/([^\/]+)\/([^\/]+)\/([^\/]+)\/? Bad-Debt-Recovery-in/?zipcode=$1&location=$2+$3 [QSA,L,NC]
You can use the WP function sanitize_title
Combine this with a php loop i.e.
$url = $_SERVER['REQUEST_URI'];
foreach($_GET as $g){
$url .= '/'.$g;
}
$url = sanitize_title($url);

HTACCESS friendly url and allow GET method

My code is
Options -Multiviews
RewriteEngine On
RewriteBase /
RewriteRule ^([a-z0-9-]+)\.html$ /index.php?cat=$1 [L]
If I access
mysite.com/name-of-category.html
it works, but if I access
mysite.com/name-of-category.html?anything=something
it shows the webpage but $_GET["anything"] shows nothing.
You must specify an option called QSA or 'Query String Append':
RewriteRule ^([a-z0-9-]+)\.html$ /index.php?cat=$1 [L,QSA]
It will ensure that the original query strings are also included as part of your new URL.

Categories