I'm trying to determine the correct syntax for an Apache rewrite to send visitors to a different page if a query string value does not equal a certain format.
URL 1: http://www.foo-bar.xqx/?video=123 (route one way)
URL 2: http://www.foo-bar.xqx/?video=fgh (route differently)
The format for the value should be all digits \d{0,x} but nothing I've tried so far seems to be returning as I'm expecting. The most current version for the rewrites is as follows:
RewriteCond %{QUERY_STRING} ^video=[^\d](\d{0,})$
RewriteRule - /404.php$1 [NC,L]
Use ! to negate a rule
RewriteCond %{QUERY_STRING} ^video=
RewriteCond %{QUERY_STRING} !^video=\d+$
RewriteRule - /404.php$1 [NC,L]
That will only rewrite requests that have ?video=, but only if there's no number after the =.
Related
I am trying to get the variable from my url but having problems.
the default URL is http://localhost/category.php?category_slug=gaming
With my htaccsess file
RewriteRule ^category/([\w\d-]+)$ /category.php?category_slug=$1 [L]
RewriteCond %{QUERY_STRING} category_slug=([\w\d-]+)
RewriteRule ^category$ %1 [R,L]
I get my desired URL http://localhost/category/gaming
but now I am needing to get the page variable from the url http://localhost/category/gaming?page=2
in category.php i have $page=$_GET['page'];
but when I echo it out i get Warning: Undefined array key "page" in F:\Server\htdocs\category.php on line 5
The closest i can get is with the following code
RewriteRule ^category/([\w\d-]+)$ /category.php?category_slug=$1&page=$2 [L]
RewriteCond %{QUERY_STRING} category_slug=([\w\d-]+)
RewriteRule ^category$ %1 [R,L]
I get no errors but the data isnt showing
The desired URL would be either
http://localhost/category/gaming?page=2 or http://localhost/category/gaming/page2/
im pretty sure my lack of knowledge of editing my htaccsess file is to blame. and need someone to point me in the right direction please
RewriteRule ^category/([\w\d-]+)$ /category.php?category_slug=$1 [L]
You need to add the QSA (Query String Append) flag to the first rule that rewrites the request. This appends/merges the query string on the request (ie. page=2) with the query string you are including in the substitution string (ie. category_slug), otherwise the query string in the substitution replaces the query string on the request.
(If, however, you are not including a query string in the substitution string then the original query string on the request is passed through by default - no need for the QSA flag in this case.)
Minor point, but the \w shorthand class already includes digits, so the \d is superfluous.
You should also make sure that MultiViews is disabled, otherwise all query string parameters will be lost.
For example:
Options -MultiViews
RewriteRule ^category/([\w-]+)$ /category.php?category_slug=$1 [QSA,L]
This then handles both /category/gaming and /category/gaming?page=2.
Aside:
RewriteCond %{QUERY_STRING} category_slug=([\w\d-]+)
RewriteRule ^category$ %1 [R,L]
Your second rule isn't actually doing anything, unless this is related to something else. This would likely result in a malformed redirect if it did anything at all?
This rule would redirect a URL of the form /category?category_slug=gaming to gaming (only) - which is dependent on you having a RewriteBase defined. Is that the intention?
I'm trying to complete the following rule in my .htaccess.
I have this .htaccess in root directory of domain1.com and want to arrange the following redirect domain1.com/index.php?domain2.com?page.html which will bring all visitors to domain2.com/page.html.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^
RewriteRule (.+)\.php\?(.+)\?(.+) http://$2/$3 [R=301,L]
Solved.
domain1.com/domain2.com/page.html
RewriteEngine On
RewriteCond %{HTTP_HOST} ^
RewriteRule (.+)\/(.+) http://$1/$2 [R=301,L]
Instead of constructing your initial URLs like domain1.com/index.php?domain2.com?page.html - which requires you to capture two separate backreferences (the second ? will be URL encoded). It would be better to format your URL more conventionally, like the following instead:
domain1.com.com/index.php?redirect=domain2.com/page.html
(Not sure why you were substituting a ? for the first slash in the URL being passed?)
Then you could do something like the following. Note that you need to use a condition that checks against the QUERY_STRING server variable.
RewriteCond %{QUERY_STRING} ^redirect=([^&]+)
RewriteRule ^index\.php$ http://%1 [QSD,R,L]
%1 is a backreference to the last matched CondPattern.
The QSD flag is required to discard the original query string from the request.
HOWEVER, you need to perform additional validation on the domains that can be redirected to, otherwise this will likely be abused for malicious intent once it is discovered.
UPDATE:
RewriteCond %{HTTP_HOST} ^
RewriteRule (.+)\/(.+) http://$1/$2 [R=301,L]
The RewriteCond directive here isn't doing anything. If you are wanting to validate that a Host header is present (ie. is not a HTTP/1.0 request) then you should change the regex ^ to . (simply a dot). However, if you are on a shared server then this test is probably redundant.
By using the URl-path, this will "break" if the destination hostname is the same as the source.
I'm trying to take a URL with multiple querystring parameters into a folder structure URL and am getting a 500 internal server error.
My incoming URL looks like
www.fivestarprofessional.com/ag?PY=16&PF=wm&MKT=Delaware
My destination URL I need written as
delaware.fivestarprofessional.com/16/wm/index.html
I created 3 RewriteCond statements to capture the parameters and a RewriteRule to create the output
RewriteCond %{QUERY_STRING} !^(PY=.*)$ [NC]
RewriteCond %{QUERY_STRING} !^(PF=.*)$ [NC]
RewriteCond %{QUERY_STRING} !^(MKT=.*)$ [NC]
RewriteRule %3.fivestarprofessional.com/%1/%2/index.html
Any assistance would be greatly appreciated.
You can use this rule for your redirection:
RewriteCond %{QUERY_STRING} (?:^|&)PY=([^&]+) [NC]
RewriteCond %1::%{QUERY_STRING} (.*?)::(?:|.*&)PF=([^&]+) [NC]
RewriteCond %1/%2::%{QUERY_STRING} (.*?)::(?:|.*&)MKT=([^&]+)
RewriteRule ^ag/?$ http://%2.fivestarprofessional.com/%1/index.html? [L,NC,NE,R=302]
% variables are captured only from the most recent condition using same %{QUERY_STRING}
Try this one:
RewriteCond "%{QUERY_STRING}" "^PY=([^&]+).*&PF=([^&]+).*&MKT=([^&]+)" [NC]
RewriteRule "www.fivestarprofessional.com/ag" "%3.fivestarprofessional.com/%1/%2/index.html"
The following issues have been corrected:
RewriteRule takes 3 arguments instead of 2.
Arguments to RewriteCond hadn't been enclosed in quotes.
The backreferences into the RewriteCond rules ...
... only refer to the last condition matched (pointed out by #anubhava, being documented in the apache httpd docs). Therefore, the rules need to be collapsed in one.
... referred to the parameter name plus the complete remainder of the query string
... would have matched against query string starting with the respective ul parameter due to the use of the ^ anchor.
... would have been empty anyway, as the rules fired when the patterns did not match ( use of ! prefix )
Recommended reading is the proper section of the apache httpd documentation.
The solution as it stands assumes that...
the query string starts with the url parameter PY
the order in which the url parameters PY, PF, MKT appear in the query string is fixed.
I would like to change any url containing the query string &preview_nonce= with any value passed to preview_nonce.
I understand that I have to rewrite based on the condition:
RewriteCond %{QUERY_STRING} ^preview_nonce=(.*)$
But I'm getting caught up only removing that from the query string and leaving everything else, including query strings.
Example URLs:
about-us/history/commitment/?preview=true&preview_id=9999&preview_nonce=9x323k1
stories/companies/?preview=true&preview_id=8888&preview_nonce=c448s88
The desired results:
about-us/history/commitment/?preview=true&preview_id=9999
stories/companies/?preview=true&preview_id=8888
You can use this rule for query string removal:
RewriteCond %{QUERY_STRING} ^(.+?&)?preview_nonce=[^&]*(?:&(.*))?$ [NC]
RewriteRule ^ %{REQUEST_URI}?%1%2 [L,NC,R=302]
Make sure this rule is placed before other WP rules.
I want to get get sort_by in my php code but its seems to be that this is returning value of variable 1 with digit 0.
RewriteRule ^abc/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ search.php?counrty=abc&state=$1&gender=$2&r_city=$3&r_mstatus=$4&r_religion=$5&r_ethnicity=$6&r_cast=$7&r_profession=$8&r_education=$9&sort_by=$10 [NC]
According to Apache documentation, for $N, the value of N can only be 0-9. So trying to get $10 is not going to work. You will likely need to go through two rewrite steps. So, perhaps you use something like this:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+/[^/]+/[^/]+)/?$ search.php?country=$1&state=$2&gender=$3&r_city=$4&r_mstatus=$5&r_religion=$6&r_ethnicity=$7&r_cast=$8&r_profession=$8&profession_and_education_and_sort=$9 [NC]
RewriteCond %{REQUEST_FILENAME} ^search\.php$
RewriteCond %{QUERY_STRING} ^(country=.*&state=.*&gender=.*&r_city=.*&r_mstatus=.*&r_religion=.*&r_ethnicity=.*&r_cast=.*)&profession_and_education_and_sort=([^/]+)/([^/]+)/([^/]+)
RewriteCond ^search\.php$ search.php?%1&r_profession=%2&r_education=%3&sort_by=%4 [NC,L]