Mod_rewrite with ".html?variable=" not working [duplicate] - php

This question already has an answer here:
.htaccess RewriteRule to preserve GET URL parameters
(1 answer)
Closed 8 years ago.
I have a website that I successfully use the following url rewrite on:
RewriteRule ^(.*)_r-(.*)/calculator.html$ /calcadd.php?rest_id=$1 [L]
As my website has evolved I've decided it would be helpful to be able to pass some additional parameters to this page. So I tried to update the url to include a variable after the .html. Here's what I tried:
RewriteRule ^(.*)_r-(.*)/calculator.html?ids=(.*)$ /calcadd.php?rest_id=$1&ids=$3 [L]
This doesn't work, I get a page not found error. And just in case it wasn't clear, this is NOT a mod_rewrite isn't enabled or whatever problem. All my other rewrites work just fine, so mod_rewrite is enabled, allow override is ALL, etc.
Thanks!

You just need a QSA flag here to preserve original query while adding new query parameters:
RewriteRule ^(.*)_r-(.*)/calculator\.html$ /calcadd.php?rest_id=$1 [L,NC,QSA]
Also remember that you cannot match query string in RewriteRule.

The RewriteRule only consideres the requested url without the querystring by default. You need a RewriteCond to achieve what you want:
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^(.*)_r-(.*)/calculator.html$ /calcadd.php?rest_id=$1&%1 [L]
untested.
With %1, %2, etc. you can inject the patterns matched in the correspondent parenthesized patterns in the RewriteCond statement in your RewriteRule.

Just leave it the way it was and use this. QSA flag means Query String Append. It will will automatically add the query sting to the end of your rewrite.
RewriteRule ^(.*)_r-(.*)/calculator.html$ /calcadd.php?rest_id=$1 [QSA,L]

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

URL Mod Rewriting

I am using codeigniter for my website, and before theres always that index.php? on my every url or links, for example
mysite.com/index.php?/about
Google has indexed all of my urls with that index.php? and I want to remove it and redirect it without that. I am having a problem rewriting the URL and redirect it to mysite.com/about and this what i have tried so far
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?(/[^\s\?]+)? [NC]
RewriteRule ^ %1/ [QSA,L,R=301]
what happened is, it only removed the index.php,
for example mysite.com/index.php?/about will turn to mysite.com/?/about I don't know how to remove that question mark,
I'm not good on mod_rewrite thanks in advance for the help.
I think you can improve the rules slightly.
RewriteCond %{REQUEST_URI}?%{QUERY_STRING} index\.php?\?.+
RewriteRule .*$ %{QUERY_STRING}? [R=301,L]
Essentially, you don't have to worry about the entire request line in %{THE_REQUEST}, which removes all the complicated regex. Also, the rule redirects to whatever is listed in %{QUERY_STRING}, and removes the query string.
I am not sure why you used QSA in the first place. I think that was part of the problem earlier. Just for an exercise, you can try removing QSA and see what happens.
You should try this one.
RewriteCond %{QUERY_STRING} ^/[a-z]+$ [NC]
RewriteRule ^/index.php$ %{QUERY_STRING} [NC,L,R=301]

mod_rewrite single "directory" retain the rest of the URL vars [duplicate]

This question already has answers here:
RewriteRule that preserves GET parameters
(3 answers)
Closed 8 years ago.
Can I have a single URL var get aliased to a directory, then any additional URL variables stay in the string?
Example:
If I have a contact page and want to swap the "regarding" select based on a quick URL variable.
?page=contact => loads the contact page with all defaults
?page=contact&regarding=bug => loads the contact with the regarding select option for a bug report automatically selected
#Enable rewrite engine
RewriteEngine On
#contact,contact/ -> page=contact
RewriteRule ^contact?$ ?page=contact [L]
RewriteRule ^contact/?$ ?page=contact [L]
This works fine for domain/contact/, but if I add additional URL variables they get dropped, even if they appear in the URL.
I understand I could do something like:
RewriteRule ^contact/bug/?$ ?page=contact&regarding=bug [L]
but I would rather have it accept any number of flags in case I wanted to do extra stuff there.
I would like to be able to have domain.com/contact/ and domain.com/contact/?regarding=bug
Appending the QSA flag is what I was after. Thanks to mario above for pointing that out.
For reference, the code would be:
#Enable rewrite engine
RewriteEngine On
#contact,contact/ -> page=contact
RewriteRule ^contact?$ ?page=contact [L,QSA]
RewriteRule ^contact/?$ ?page=contact [L,QSA]

How to escape “?” using regex in .htaccess for mod_rewrite

What is wrong in this rule?
RewriteRule ^page\?v=([^/]+)$ page.php?v=$1 [L,NC]
I just want to make the URL looks like that
http://www.domainname.com/page?sk=info
You don't have to include the query parts if they're not changing anyway.
RewriteRule ^page$ page.php [L,NC]
The RewriteRule will not match any part of the query string. page?v=123 will still become page.php?v=123
Also, your RewriteRule uses ?v= while you talk about ?sk=info
Also, you can find additional information about this mod_rewrite case here:
http://wiki.apache.org/httpd/RewriteFlags/QSA
And another SO entry about this issue here.

Categories