htaccess regex not accepting some special characters - php

I have a PHP script located at http://localhost/dir1/dir2/shift.php that I am passing urls as parameters to like so: http://localhost/dir1/dir2/https://google.com but I am getting a Forbidden you don't have access error. When I remove the : it works fine though. I have checked my regex with some online regex validation tools and it says that it should be working but it doesn't
RewriteEngine on
RewriteRule ^dir1\/dir2\/([A-Za-z0-9\!\#\#\$\%\^\&\*\(\)\_\-\+\=\{\}\[\]\;\:\'\"\<\,\>\.\?\|\~\`\s\/\\]+)\/? dir1\/dir2\/shift.php?url=$1 [L]
Any help would be great. Thanks!

You should not use reserved characters in URL.
Though you can URL encode and then pass it as a parameter. For example:
http://localhost/dir1/dir2/shift.php?
param=http%3A%2F%2Flocalhost%2Fdir1%2Fdir2%2Fhttps%3A%2F%2Fgoogle.com
And then in shift.php you can first URL decode the parameter and then use.

Related

Why do I have a zero in my $ _GET variable when I rewrite URLs?

I post here because despite the many topics on the net I have not managed to solve my problem.
I concise a website, and to optimize SEO, I must make the URL Rewriting.
I have GET variables passing in the URL and some have spaces that are encoded in the URL by "%20", for example:
mapage.php?produit=aménagements%20bois
So I apply my rewrite rule in the .htaccess file:
RewriteRule ^ma-page-amenagements-bois$ mapage.php?produit=aménagements%20bois [L]
The problem is that URL rewriting worked but a zero appears in my variable $ _GET instead of space ("aménagements0bois" instead of "aménagements bois") when I try the new URL, which distorts the dynamic display of my page.
I would like to know how to solve this problem.
Thank you
You don't need to add encoded characters in your rewrite rule, you can escape spaces with \:
RewriteRule ^ma-page-amenagements-bois$ mapage.php?produit=aménagements\ bois [L]
The reason you get a 0 in your url is because apache uses %1, %2, ... as rewrite variables. And because you don't have a %2, only the 0 remains.

Why does my rewriterule captures parts from the substitution string instead of the uri

Given this url:
http://test.com/myfile/product/1
and the following RewriteRule:
RewriteRule ([^/.]+)/?(.*) app/$1.php?$2
I would expect the url to become:
http://test.com/app/myfile.php?product/1
and it does when I use an online htaccess tester. But on my local dev environment I get this:
The requested URL /app/app.php was not found on this server.
Why? This can't be right, right? I suspect it is a bug caused by my setup (docker containers and dinghy-http-proxy) but since I am new to this rewriting I am not sure.
Try this:
RewriteRule ^([^/.]+)/?(.*) app/$1.php?$2
The problem is that the regex can match anywhere in the path string, and since the / is optional the result is unlikely to be what you want.
Also, make sure that you don't have multiple rewrite rules which apply, they will all get processed by default!

htaccess RewriteRule url frendly searches

I have a .htaccess file with this in it:
RewriteRule ^search/([a-zA-Z]+)$ index.php?page=search&search=$1
So basically it sends URLs like this:
url.net/search/this
To this:
url.net/?page=search&search=this
But when I send it a URL like:
url.net/search/this+search
I get an error returned as it doesn't know how to deal with +search bit.
Is there a way I can get it to include the + between words when the user clicks search?
I want it so that if the user types i+want+this+or+that or this+is+what+i+want+to+find, so no mater how long it is, it knows how to handle the parse to $_GET['search'] parameter.
You should be able to just include it in the regex...just remember to escape it,
RewriteRule ^search/([a-zA-Z\+]+)$ index.php?page=search&search=$1
Try this regex for the rewire rule:
RewriteRule ^search/([a-zA-Z].+)$ index.php?page=search&search=$1
Note the . before the + sign. Works as a regex here on this live PHP regex site. Yes, I know this is an Apache rewrite rule & PHP has no role at this stage, but basic regex logic should remain the same.

PHP/Apache - Building a different URL, can it be done?

I'm looking into how to build a different URL, and I was wondering if it is possible to replace the query-string/question mark symbol PHP uses in URLs which is the '?' into something else? For example an exclamation mark '!'
www.example.com!foo=1
Is this possible?
Sure, but you'll need rewrite rules to tell the server how to serve them. And PHP won't parse them automatically for you, so $_GET/$_POST/$_REQUEST variables won't get set and you'll have to parse them yourself from $_SERVER['REQUEST_URI'].
You don't have to use mod_rewrite for this, because you can still interpret the url with $_SERVER["PATH_INFO"] or $_SERVER["REQUEST_URI"] depending on your configuration. Just split it at the ! with explode and parse it with parse_str. It is your decision if this makes sense.
You should be able to replace it by htaccess too but I am unable to test it because I have no apache running atm. Must be something like this.
RewriteEngine on
RewriteBase /
RewriteRule ^!(.*)$ http://domain.com/?$1 [R,NC]
If this works, remove the "R," from the rule.

Trouble with encoding special chars for URL through text input

I'm building a PHP application using CodeIgniter. It is similar to Let Me Google That For You where you write a sentence into a text input box, click submit, and you are taken to a URL that displays the result. I wanted the URL to be human-editable, and relatively simple. I've gotten around the CodeIgniter URL routing, so right now my URLs can look something like this:
http://website.com/?q=this+is+a+normal+url
The problem right now is when the sentence contains a special character like a question mark, or a backslash. Both of these mess with my current .htaccess rewrite rules, and it happens even when the character is encoded.
http://website.com/?q=this+is+a+normal+url? OR
http://website.com/?q=this+is+a+normal+url%3F
What does work is double-encoding. For example, if I take the question mark, and encode it to %253F (where the ? is encoded to %3F and the % sign is encoded to %25). This url works properly.
http://website.com/?q=this+is+a+normal+url%253F
Does anyone have an idea of what I can do here? Is there a clever way I could double encode the input? Can I write a .htaccess rewrite rule to get around this? I'm at a loss here. Here are the rewrite rules I'm currently using for everyone.
RewriteEngine on
RewriteCond %{QUERY_STRING} ^q=(.*)$
RewriteRule ^(.*)$ /index.php/app/create/%{QUERY_STRING}? [L]
Note: The way CodeIgniter works is they have a index/application/function/parameter URL setup. I'm feeding the function the full query string right now.
If your’re using Apache 2.2 and later, you can use the B flag to force the backreference to be escaped:
RewriteCond %{QUERY_STRING} ^q=.*
RewriteRule ^ /index.php/app/create/%0? [L,B]
I usually do human readable urls like this
$humanReadableUrl= implode("_",preg_split('/\W+/', trim($input), -1, PREG_SPLIT_NO_EMPTY));
It will remove any non-word characters and will add underscores beetween words

Categories