htaccess change get variable to mysql username database - php

I was wondering if I'm supposed to use .htaccess to change a get variable to a value from the database (username).
so if there is
http://www.url.com/user.php?u=1
how do you conver it to
http://www.url.com/andrewliu
Thanks!

You need to change your user.php to take a username instead of a userid. Then you can use something like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?([a-z0-9_-]+)/?$ /user.php?u=$1 [L]
This passes the username through the u query string parameter, essentially: /user.php?u=andrewliu
Otherwise there's no way htaccess and mod_rewrite can know what the mapping is between user_id and username. Alternatively, you can write a database script and use RewriteMap to create a mapping for you:
RewriteMap usermap prg:/path/to/userscript
and in your htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?([a-z0-9_-]+)/?$ /user.php?u=${usermap:$1} [L]
The last option, if you've only got, say, 5 users (or some small amount), you can make explicit rewrites:
RewriteEngine On
RewriteRule ^/?andrewliu$ /user.php?u=1 [L]
RewriteRule ^/?anotheruser$ /user.php?u=2 [L]
RewriteRule ^/?foousername$ /user.php?u=3 [L]

You need to use Mod-ReWrite. You can use this in your htaccess files but if you have access to your httpd.conf file it will prove to be quicker there.
You would ideally use the ?u=1 in your htaccess file and use that to find the the name and append the name onto the end. Otherwise you will have to search for the username and could be succesiptable to spelling mistakes etc etc.
RewriteRule ^/([0-9]+)/.*$ /user.php?u=$1 [L]
This is how Stackoverflow does it, try accessing this page without the number in the url and you will get a 404 page not found. Get the number right and what ever else you type after the forward slash will be exchanged for the correct words! This is much more convienent!

Use $GET in php to get the value of 'u'
Then use your logic to convert it to name.
Now just use redirect function to go to to that URL

Related

Constructing a .htaccess file to hide a string in certain cases

I'd like to be able to hide a $_REQUEST variable name in a visitor's address bar, and also automatically append the variable name to all data requests, but only when the .php extension is not included.
Currently requests look like this:
example.com/?page=request
I'd like them to look like this
example.com/request
The problem is, domains like this still need to work:
example.com/mail.php
So I figure I'd like all requests to files that don't end in the extension .php to invisibly forward to the contents of
example.com/?page=*
While actually displaying the address:
example.com/*
Here's what I have so far:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^page=-
RewriteRule ^(.*)$ /$1? [L,R]
But this doesn't even replace the string when it's entered.
I wouldn't mind actually having to add the name of each accessible .php file to the .htaccess file, this would probably build on security.
You probably want to forward /request URI to /?page=request. If thats the case use this rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^].]+)$ /?page=$1? [L,QSA]

Is it possible to query a database using a value passed in a URL, and write the result of the query to the URL using mod_rewrite?

Is it possible to use mod_rewrite to write an htaccess rule that takes a url parameter value (for example: id=1, where 'id' is the parameter, and '1' is the parameter value), query a database with the parameter value specified, and then write the value returned from the query as a part of the url of the requested page?
I know the basics of mod_rewrite, for example rewriting a url that appears like:
www.example.com/item.php?id=1
to the following:
www.example.com/item/1
An example of what I would require is writing the following url:
www.example.com/item.php?id=1
to this:
www.example.com/item/name-of-item-based-on-id-specified-in-original-url
However I have no idea if what I am looking to do is possible using mod_rewrite.
If anyone has a solution to this problem I'd be very grateful if you could help me. If what I am trying to do is not possible using htaccess and mod_rewrite, can someone please point me in the direction of how I may go about solving this problem?
It's possible, but you need to use a RewriteMap to define a mapping that you can use within a RewriteRule.
Apache version 2.2 doesn't have direct database access so you'll need to write a script that does the actual query then return the result. You can define this map using the "External Rewriting Program".
So if you have a script that takes "cats" from stdin, then queries the database, and returns "1", you'd define it like so:
RewriteMap item_lookup prg:/path/to/item_lookup.php
That directive has to be in your server or vhost config, it can't be in an htaccess file. But you can use the mapping in an htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /item.php?id=${item_lookup:$1} [L]
So this takes the URI /cats and rewrites that to /item.php?id=1.
If you are using apache 2.4, then you can take advantage of the "DBD" map. You can insert a query right into the map definition, bypassing having to use an external script. You'd use it in the same way.
RewriteMap item_lookup "fastdbd:SELECT id FROM items WHERE name = %s"
Then use it in the same way:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /item.php?id=${item_lookup:$1} [L]
Without using a DBD/FastDBD query, I think you're honestly better off just doing the database lookup from item.php, since you'd be duplicating all of that work in a second external script anyways. Just add something like:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^item/([0-9]+)$ /item.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9-]+)$ /item.php?name=$1 [L]
And in your item.php script, check for both id and name. If you have a name, do the database lookup in order to turn that into an id. It's much easier to manage, you don't need to have server/vhost config access, and you're not complicating matters by using a rewrite map.

Mod_ReWrite regex unknown variables count?

i store uploaded files at /storage/ this way
public-adam-luki-uploads-123783.jpg
park-hanna-adel-propic-uploads-787689.jpg
the '-' count unknown because it slice the pic description
i want my users to be able to access it as
http://site.com/public/adam/luki/uploads/123783.jpg
http://site.com/park/hanna/adel/propic/uploads/787689.jpg
i think it is the same problem here
mod_rewrite with an unknown number of variables
but i can't do it because i'm new to mod_rewrite module
i hope you can help me guys with the right rewriterule
The question you link to doesn't actually do what you are trying to do (although the principle is the same) what they do is convert the url to GET variables.
If all you want to do is convert / to - then you can use a simple rewrite rule that will run in a loop:
ReWriteRule ^(.*)/(.*)$ $1-$2 [L]
There are of course a few caveats to that...
Firstly, even if you are trying to get to a real directory/file the rule will still switch out / and - and leave you with a 404. You can get around that by adding conditions; to stop it rewriting real files:
RewriteCond %{REQUEST_FILENAME} !-f
You would do better however to limit the matches to only images (jpgs):
RewriteCond %{REQUEST_FILENAME} !-f
ReWriteRule ^(.*)/(.*)\.jpg$ $1-$2.jpg [L]
Preferred Solution
ReWriteCond %{REQUEST_FILENAME} !-f
ReWriteRule ^images/(.*)/(.*)uploads[-/](\d+)\.jpg$ images/$1-$2uploads-$3.jpg [L]
RewriteCond %{REQUEST_FILENAME} !-f
ReWriteRule ^images/(.*)$ storage/$1 [L]
This solution requires you to use urls like:
http://site.com/images/park/hanna/adel/propic/uploads/787689.jpg
The pseudo directory images means you can be sure that the url is actually one that you want to redirect and it doesn't break other images/links on your site.
The above rules take a url (like the example above) and transforms it like so:
images/park/hanna/adel/propic/uploads/787689.jpg <--- Original
images/park-hanna/adel/propic/uploads-787689.jpg
images/park-hanna-adel/propic/uploads-787689.jpg
images/park-hanna-adel-propic/uploads-787689.jpg
images/park-hanna-adel-propic-uploads-787689.jpg
storage/park-hanna-adel-propic-uploads-787689.jpg <--- Final

Rewrite URL Parameter containing dot

I think that I am trying to achieve an impossible result.
The scenario is PURL-Mailing and I already got some URL's rewritten to fit the URL, sent to the customer.
The customer enters the site by the following domain: http://domain.com/UserName
The Variable UserName represents the GET-Variable, which equivalent to http://domain.com/index.php?user=UserName
I achieve this with the following rewrite Rules:
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?name=$1 [QSA]
#This works perfect and translates to http://domain.com/UserName
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?name=$1 [L]
#This achieves the goal but does not reflect in the URI I want:
#http://domain.com/UserName
To go further, there are also some Names containing a dot in the Name like A.Jackson that also need to be treated as UserName. As those are only 13 Name I could implement them manually. What I don't know is how I can prevent the part after the dot to be handled as a file extension. Is there a way to write a custom handle in *mod_rewrite* for those?
And if so, can anybody explain to me how?
Thanks in advance and best regards!
ok try below
RewriteCond %{REQUEST_FILENAME} !(img|anyother folders that you want to ignore|anyother folders that you want to ignore|...)
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9_-]*[\.]*[a-zA-Z0-9_-]+)[/]*$ test.php?name=$1 [L]
replace 'anyother folders that you want to ignore' with folder name that you want to ignore. Seperate each folders with '|'
You also have to provide full path to the CSS, image or any other links used in your web page when you using URL rewrite functions
Here is your fix
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9_-]*[\.]*[a-zA-Z0-9_-]+)$ index.php?name=$1 [L]

Mod rewrite user URL

I'm lost here. I'm using this script to give users the opportunity to enter their username lijke this:domain/username
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ userpage.php?user=$1 [NC,L]
</IfModule>
This works fine. However, every user has pages I must link to: Video, Music, Images etc...
So I need something like:
domain/username/video
In php code it must be something like:
user.php?user=test&page=video
And one other question: What is the preferable way to link in this situation?
userpage.php?user=test&page=video
or
/test/video
And finally: Is it possible to deny the possibility to enter the url:
domain/userpage.php?user=test&page=video? Instead just always show: domain/test/video
Thanks in advance
I'm not 100% sure what you're asking? Do you need to change the rewrite rule to match the URL site.com/moonwalker/videos? You could try this:
RewriteRule ^([^/]+)/(images|videos|music)/?$ userpage.php?user=$1&page=$2 [NC,L]
Update
Just a quick note on the domain/member/videos URL structure. That could end up causing you problems in the future. For instance what if you decide to have a single page that shows all member videos? You'd probably want to URL to look something like site.com/members/videos. That's a problem, because the rewrite rule will also match that, but "members" isn't a member username.
I would probably structure my member page URLs like site.com/user/moonwalker/videos so it doesn't clash with future rewrite rules. You would change the above rewrite rule to this:
RewriteRule ^user/([^/]+)/(images|videos|music)/?$ userpage.php?user=$1&page=$2 [NC,L]
Then later on you can add a rewrite rule like:
RewriteRule ^members/(images|videos|music)/?$ allusers.php?page=$1 [NC,L]
To show all member videos.
Yes, it is possible by looking at the request line:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /userpage\.php[?\ ]
RewriteRule ^userpage\.php$ - [F]
This is necessary as the URL path could already be rewritten by another rule and thus using just RewriteRule would match those already rewritten requests too.

Categories