Using two GET variables in .htaccess (having problems outputting) - php

I'm currently trying to add a second get variable to an existing htaccess rule. I have little experience with the htaccess but using what expressions I know, I think it's missing one element to ensure the second variable is outputted correctly.
The domain (as the user sees it);
http://domain.com/london-area-info/?src=go
The rule i'm trying to apply;
RewriteRule ^(.*)-area-info/(.*)$ regioncc.php?region=$1&src=$2
The resulting output URL;
http://domain.com/regioncc.php?region=london&src=
I appreciate I'm probably overlooking something but looking around I haven't found an explanation has to how to make sure the ?src=go is shown after the rewrite rule rather than in another directory like /xxx-area-info/src/ for example.
EDIT
I have also tried the following rule but wasn't sure if I was escaping correctly
RewriteRule ^(.*)-area-info/(\?src=?.*)?$ regioncc.php?region=$1&src=$2

I believe this is what you want:
RewriteRule ^(.*)-area-info regioncc.php?region=$1&%{QUERY_STRING}

Related

.htaccess slash in name get variable issue

Hi and sorry for bothering you, i try to look around but i couldn't find a solution, and i really can't figure out how and if it's possible to do something like that with rewriterule.
This is the portion of htaccess file interested in my problem:
RewriteRule ^pagename/([^/]+)-([^/]+) mypage.php?name=$1&id=$2 [NC,L]
example.com/mypage/dogarticle-800ad43
so my variable in php will be like
$_GET["name"]=dogarticle and $_GET["name"]=800ad43
so far so good, but if an article contain a slash i can't get the right variable anymore, example
example.com/mypage/animals/dogandcat-800ad44
where animals/dogandcat should be name i receive the following get vars:
$_GET["name"]=animals $_GET["name"]=dogandcat
Here the question what should i do to receive the following get var:
$_GET["name"]=animals/dogandcat $_GET["name"]=800ad44>
Cant really figure out the solution, probably not the best of my days.
Thanks
edit:
in the name there can be a lot of slash example:
example.com/mypage/animals/dogandcat/squirrel/someotheranimal-800ad44
You can use:
RewriteRule ^pagename/(.+)-([^/]+)$ mypage.php?name=$1&id=$2 [NC,L]

Apache mod_rewrite: rewrite a path into a query string of an existing file

I am creating a simple redirect tool for myself in PHP. I'm going to store codes that map to their respective URLs in an associative array, and then redirect anybody who inputs this code to their URL.
I am using query strings to GET the input (the name of the variable and the value, which is the code), and I already know how to make the redirect part work with PHP. The issue is, I don't want my users to have to type in a question mark, the name of the variable, and then an equals sign. I would like for them to simply type in a path that will be converted into a query string, so that PHP can process the redirection.
I was wondering how I could turn a path (an arbitrary URL path) into a query string of an existing file. I was able to find the opposite task quite easily on Stack Overflow (.htaccess - Rewrite query string and redirect to directory) but I want to be able to pass any arbitrary URL path (that does not exist in the directory) into an existing file's query string. I found an example of what I wanted (URL rewrite for converting path to querystring), but it was for multiple query strings and in this case, I only need one. I don't think the last question I referenced was successfully answered either.
How can I redirect
https://www.example.com/r/blahblahblah
into
https://www.example.com/r/index.php?r=blahblahblah
using .htaccess, so that I can parse the query string in my PHP file?
Something like this:
RewriteRule ^(.*)$ index.php?r=$1 [NC,L,QSA]
On this URL:
example.com/r/blahblahblah
From index.php:
print_r($_GET);
Will return this:
Array ( [r] => blahblahblah )
(Edit to mention this assumes .htaccess is in the /r directory)
RewriteEngine On
RewriteRule ^r/([a-zA-Z0-9]+)$ /r/index.php?r=$1

Change Of Complex To Simple URL & Remove Index.php

I have been trying & searching alot but couldn't find any solution for i have tried many things in .htaccess
My website's Url
mysite.org/torrents.php
To this
mysite.org/Browse
This
mysite.org/torrents.php?parent_cat=Movie
to this
mysite.org/Movie
This
mysite.org/account-details.php?id=737
to this
mysite.org/user/username
Your links won't all work properly, i.e. the /user/username one won't work to an ID number, you could pass the username through then do a lookup based on the username, but htaccess won't know that username "tim" is id number "123"
This should work:
RewriteEngine On
RewriteRule "user/([0-9]*)" account-details.php?id=$1
RewriteRule "category/([a-zA-Z0-9_-]*)" torrents.php?parent_cat=$1
RewriteRule "browse" torrents.php
RewriteRule "" torrents.php
With that I've separated out the categories into a subfolder called 'category' this makes it far easier to allow any category name you want.
For the regular expression matches in the RewriteRules theres an excellent free program called 'Expresso' that will help you build up regular expressions and test them against a list of values to see if they match, I'd recommend searching for it and getting a copy as it's very handy.
Also for the username part if you wanted to do username instead of user ID numbers, then swap:
RewriteRule "user/([0-9]*)" account-details.php?id=$1
to this:
RewriteRule "user/([a-zA-Z0-9_-]*)" account-details.php?username=$1
Try using this. it is a htaccess generator. If this doesn't work you will need to check you php.ini settings to see if it allows this type of link.

htaccess rewrite rule with query string, multiple variables and no php file in the link

We're trying to migrate our forum to another platform and we have encountered links which have queries in them, such as
http://forum.test/threads/119312-Warnight-CS-GO?p=2306618&viewfull=1#post2306618
which has to point to http://forum.test/threads/warnight-cs-go.119312/#post-2306618
So the logical structure of the original link is :
http://{forum_base_url}/threads/{thread-id}-{thread-permalink}?p={post-id}&viewfull={post-number-in-thread}#post{post-id}
While the new one is:
http://{forum_base_url}/threads/{thread-permalink}.{thread-id}/#post-{post-id}
So for the rewrite to work we need to "pull" three things out of the original link: the thread-id, the permalink, and the post-id. The first two aren't an issue, it's the third one which doesn't want to cooperate.
After scouring the Internet for possible solutions, we came up with:
RewriteCond %{QUERY_STRING} ^p=(\d+)&viewfull=(\d+)#post(\d+)$
RewriteRule ^threads/([0-9]+)-(.*)$ /threads/$2\.$1/#post-%1? [R=301,NC,L]
But unfortunately, the rewrite doesn't work.
What throws us off regarding the rewrite is that there are multiple variables in the query and no .php file specified in the link itself, so we can't just use the solution offered here: https://stackoverflow.com/a/2252242/1288397
Any tips on how to overcome this particular hurdle?
The #post- part of the URL is never sent to the server, it remains entirely on the browser's end, so there's no way to match against it. Luckily, the post ID is already in the query string so you can ignore it:
RewriteCond %{QUERY_STRING} ^p=(\d+)&viewfull=(\d+)$
RewriteRule ^threads/([0-9]+)-(.*)$ /threads/$2\.$1/#post-%1? [R=301,NC,L]
The other thing is that the thread-permalink is lowercase in your second URL. Not sure if that matters or not, but in order to change text to lowercase, you need to use a Rewrite Map, and the tolower internal function. You can only declare maps in the server vhost/config, so if you don't have access to those config files, you're not going to be able to use maps.
Thanks for clearing up the #post- aspect.
I tried your modification, but the links initially became:
http://forum.test/threads/warnight-cs-go.119312/%23post-2306618#post2306618
In order to work, the rewrite needs to be
RewriteCond %{QUERY_STRING} ^p=(\d+)&viewfull=(\d+)$
RewriteRule ^threads/([0-9]+)-(.*)$ /threads/$2\.$1/#post-%1? [R=301,NC,NE,L]
Notice the additional NE (noscape) flag at the end
That's because, by default, special characters, such as & and ?, will be converted to their hexcode equivalent. Using the [NE] flag prevents that from happening, as seen here: http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_ne

URL routing/rewriting and compatibility with Nginx/Lighttpd

I'm writing a simple URL routing code based on using mod_rewrite to pass the URI as a GET parameter, like Drupal does. So I have the rule:
RewriteRule ^(.*)$ index.php?q=$1 [QSA,L]
And the URL http://www.example.com/test/1 would give me "/test/1/" passed as value $_GET['q'], instead of the usual index.php/test/1 and having to extract that from $_SERVER['REQUEST_URI'].
The thing is, the mod_rewrite QSA flag allows me to still use query strings normally, which I find very useful for parameters like filters and pagination, like "/products/category/?pg=1".
Will this work the same on Nginx and Lighttpd servers? I'd like my code to be portable.
Thanks.
No, in either case you'll have to translate your rules into each server's specific syntax. Some links to get you started:
http://forums.digitalpoint.com/showthread.php?t=187965
http://redmine.lighttpd.net/wiki/lighttpd/Docs:ModRewrite
https://serverfault.com/questions/24243/nginx-support-for-htaccess-rewrite-rules-differences-from-apache

Categories