htaccess multiple query string not working - php

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?

Related

PHP GET after htaccsess rewrite

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?

Rewrite automatically removes backslash if there's more than one?

I have a very simple url rewriting rules:
RewriteEngine on
RewriteCond %{HTTP_HOST} !script.php
RewriteRule ^test/(.*) script.php?q=$1
The idea is to have this kind of urls: http://mywebsite.com/test/http://example.com
and then send http://example.com to the script.php as a query parameter. The problem is that I'm receiving http:/example.com instead of http://example.com. Also, http:////example.com would be sent as http:/example.com. What causes this behavior ?
Apache mod_rewrite engine converts multiple ///... into single / for pattern matching in RewriteRule directive. However if you match it using RewriteCond then you can match multiple /s.
You can use rule like this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/+test/+(https?://.+)$ [NC]
RewriteRule ^ script.php?q=%1 [L,QSA]
The browser causes this behaviour. It contracts a sequence of / into 1 /, because it is still essentially a path. ///// does not change the directory we are in, so we could as well use /.
You have two options:
Change your links to use a query string instead. If you rewrite test/?q=something to script.php?q=something everything works as expected. You would do the following:
RewriteRule ^test/?$ script.php [L]
Since you don't alter the query string, the original query string is automatically copied to the new query string.
Don't make an assumption on how many slashes you will encounter. The url might not look correctly in the url bar of the browser, but if it is just a redirect, it will only be visible for a very short period of time.
RewriteRule ^test/(http|https):/+(.*)$ script.php?q=$1://$2

How to use .htaccess mod rewrite to redirect php url as a string?

I've been trying to make:
http://site.com/file.php?x=foo
redirect to:
http://newsite.com/something/completely/different/
Using the following:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^file\.php?x=foo$ http://newsite.com/something/completely/different/ [R=301,NC,L]
</IfModule>
But it doesn't do anything (just loads same old page as if I never touched the htaccess).
Is there something I'm missing?
Thanks much for the help!
The ? at the end of .php is making the last p optional (so .phpx=foo or .phx=foo would match). It's not actually being considered a character in the match. Try escaping the ? like: \?
Edit:
For those wondering what the solution was:
The query string arguments aren't passed to the string that RewriteRule matches against. So you have to create a RewriteCond on QUERY_STRING first, then match the url without arguments. E.g.:
RewriteCond %{QUERY_STRING} x=foo
RewriteRule ^file\.php$ http://newsite.com/something/completely/different/ [R=301,NC,L]
There's probably/hopefully a better way to do this (since this RewriteCond would cascade down to other rules, which is annoying).

Wordpress Won't Keep My Url Params

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.

PHP all GET parameters with mod_rewrite

I am designing my application. And I should make the next things. All GET parameters (?var=value) with help of mod_rewrite should be transform to the /var/value. How can I do this? I have only 1 .php file (index.php), because I am usign the FrontController pattern. Can you help me with this mod_rewrite rules?Sorry for my english. Thank you in advance.
I do something like this on sites that use 'seo-friendly' URLs.
In .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /index.php [L]
Then on index.php:
if ($_SERVER['REQUEST_URI']=="/home") {
include ("home.php");
}
The .htaccess rule tells it to load index.php if the file or directory asked for was not found. Then you just parse the request URI to decide what index.php should do.
The following code in your .htaccess will rewrite your URL from eg. /api?other=parameters&added=true to /?api=true&other=parameters&added=true
RewriteRule ^api/ /index.php?api=true&%{QUERY_STRING} [L]
.htaccess
RewriteEngine On
# generic: ?var=value
# you can retrieve /something by looking at $_GET['something']
RewriteRule ^(.+)$ /?var=$1
# but depending on your current links, you might
# need to map everything out. Examples:
# /users/1
# to: ?p=users&userId=1
RewriteRule ^users/([0-9]+)$ /?p=users&userId=$1
# /articles/123/asc
# to: ?p=articles&show=123&sort=asc
RewriteRule ^articles/([0-9]+)/(asc|desc)$ /?p=articles&show=$1&sort=$2
# you can add /? at the end to make a trailing slash work as well:
# /something or /something/
# to: ?var=something
RewriteRule ^(.+)/?$ /?var=$1
The first part is the URL that is received. The second part the rewritten URL which you can read out using $_GET. Everything between ( and ) is seen as a variable. The first will be $1, the second $2. That way you can determine exactly where the variables should go in the rewritten URL, and thereby know how to retrieve them.
You can keep it very general and allow "everything" by using (.+). This simply means: one or more (the +) of any character (the .). Or be more specific and e.g. only allow digits: [0-9]+ (one or more characters in the range 0 through 9). You can find a lot more information on regular expressions on http://www.regular-expressions.info/. And this is a good site to test them: http://gskinner.com/RegExr/.
AFAIK mod_rewrite doesn't deal with parameters after the question mark — regexp end-of-line for rewrite rules matches the end of path before the '?'. So, you're pretty much limited to passing the parameters through, or dropping them altogether upon rewriting.

Categories