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
Related
Let's call my site:
www.example.com
and I have a PHP file like this:
www.example.com/product.php?id=50
I would like to access it by using
www.example.com/product/50
but ALSO, very important, I have several subdirectories like
www.example.com/subsite/product.php?id=50
www.example.com/subsubsite/product.php?id=50
That must become
www.example.com/subsite/product/50
www.example.com/subsubsite/product/50
How can I solve it at best with PHP and .htaccess using mod_rewrite?
I banged my head with other questions like this one but to no avail.
I can't seem to find a solution that works flawlessly, taking care of all imported files like CSS, JS and PHP classes.
Ok so this might not be the complete answer but should help you find your way.
You can use regex to match your desired path pattern. So for example your htaccess might look something like...
# Check if module is installed
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Check query for matching pattern and pass id, but also append additional query strings
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\/]+\/)?product\/([0-9]+)$ /$1product.php?id=$2 [L,QSA]
# If not file or directory on server, send to 404.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /404.php [L]
</IfModule>
And what this does is...
1. Match the uri with a regex pattern
Regex: ^([^\/]+\/)?product\/([0-9]+)$
^ - Start of string.
([^\/]+\/)? - matches any directory (if exists) and stores it for reuse.
product\/([0-9]+) - Your desired path e.g. product/50 and stores the number "id" for reuse.
$ - End of string.
2. Pass captured directory and id to our file
Like so: /$1product.php?id=$2 [L,QSA]
$1 is our directory name including the trailing slash e.g. subsubsite/
$2 is our product id e.g. 50
[L,QSA] The QSA flag means we can access additional query string parameters e.g. /product/50?show=all&updated=1. More about flags can be found here http://httpd.apache.org/docs/current/rewrite/flags.html#flag_qsa
3. 404 anything not matching
Like so:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /404.php [L]
!-f If request is not a file
!-d If request is not a directory
/404.php The file used for presenting a 404 error.
Getting the id...
With the above, you can get the ID within your product.php file like so:
$id = (int)$_GET[ 'id' ];
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.
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]
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
I need to be able to shorten my page from:
mydomain.com/mixtape.php?mixid=(WHATEVER NUMBER)
To:
mydomain.com/m/(WHATEVER NUMBER)
Now usually this wouldn't be much of an issue for me to figure out, but becasue of a few pre-existing functions in my .htaccess file, it is really hard for this function not to improperly interact with the others.
Below is the current code of my .htaccess file (AND NONE OF IT CAN CHANGE)
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ profile.php?user=$1 [L]
Above, the .htaccess file is shorting my
mydomain.com/profile.php?username=(USERNAME)
to
mydomain.com/(USERNAME)
Is there anyone out there than can help me by being able to shorten the m/index.php?mixid and not have it conflict with the pre-existing function?
Prepend this rule to your .htaccess block rewriting the profile url (after turning the rewrite engine on) :
RewriteCond $1 ^m/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ mix.php?id=$1 [L]
That rule will now only be used for URLS like :
mydomain.com/m/(WHATEVER NUMBER)
The first line is a condition that the incoming URL must start with m/
The second and third lines are conditions that the incoming URL does not represent an actual file or folder (we wouldn't want our humble rewrite rule to block us from a real resource).
The fourth line is the actual rule itself witch uses a regular expression syntax to match and capture everything that appears after host name and passes it to the mixtape.php file as a GET parameter called id. This line also contains the [L] flag which states that no more rules or rewriting will occur on the current incoming URL.
In your mix.php file you can use the explode method to split the resulting string into an array :
http://example.com/m/foo/bar =>
`http://example.com/mixtape.php?id=/m/foo/bar
$splitArr = explode('/',$_GET['id']);
$splitArr =>
array (
0 => 'm',
1 => 'foo',
1 => 'bar',
)
and remove the initial m with
array_shift();
Then you are left with $splitArr containing all the parts of your URL, split with a / (slash) delimiter.
The URL example.com/m/foo/bar would look like :
array (
0 => 'foo',
1 => 'bar',
)
It is important to place this rule before the existing one as the existing rule will act on any incoming URL. The final two rules that you have should appear like this :
RewriteEngine on
RewriteCond $1 ^m/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ mix.php?id=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ profile.php?user=$1 [L]
Regarding your statement :
AND NONE OF IT CAN CHANGE
I would seriously recommend that you consider implementing a small change on that first rule. Making the final url something like mydomain.com/users/(USERNAME) (as they do here). In these cases it is much better to be more specific than overly general (as the current rule is). Have you considered the confusion that could be created if someone was to chose a user name such as :
about
faq
home
While perfectly valid usernames these users profiles would be :
mydomain.com/about
mydomain.com/faq
mydomain.com/home
Those usernames will block important URLs that you might want to save for other locations on your site. I think it is clear why those user names would be undesirable.
RewriteRule ^m/([0-9]+)$ /mixtape.php?mixid=$1
Put in before or after the existing rule. Should not cause any conflict.