Very simple mod_rewrite help - .htaccess

I've got a directory called fb and a script inside called like.php. I'd like to have the get-id passed to the like-file using mod_rewrite.
mypage.com/fb/like.php?id=5 would be mypage.com/fb/like/5
My (not working) htaccess looks like this:
RewriteEngine on
RewriteRule /fb/like/([0-9]+) /fb/like.php?id=$1
Does anyone see what's wrong here?

Try removing the slash at the beginning of your match and replace patterns like so:
RewriteRule fb/like/([0-9]+) fb/like.php?id=$1
mypage.com/ is the domain name, so the string that gets matched is fb/like/5
Also consider using the carat at the start of your match string so that it will match fb but not fffb:
RewriteRule ^fb/like/([0-9]+) fb/like.php?id=$1
Here's a short guide to mod_rewrite I've found helpful.
Edit for your follow-up question:
To match mypage.com/something/fb/like/5, you can do this:
RewriteRule ^([^/]+)/fb/like/([0-9]+) $1/fb/like.php?id=$2
This saves the first directory as $1. [^/]+ means match one or more characters that are not a slash. Put this .htaccess file in the root directory of your domain.
Alternatively, you can use the second-to-last rule and put that .htaccess file in the "something" subdirectory. Hope that makes sense.
Or you can write a rule to match simply like.php/([0-9]+) so that it'll work no matter what the directory path looks like. You can go even more generic and make this apply to any PHP file, not just like.php. It really depends on how you want your site to work.

Related

URL redirect to use subdirectory as GET parameter

I imagine this has to be a common scenario but I'm struggling to describe it sufficiently well or to find a working answer!
Essentially I want to make hundreds of URLS that include unique reference codes but that are easy to type in the form example.com/aabbcc, which will be intercepted and all delivered to a PHP script for validating that code, located somewhere like example.com/script.php.
I need the subdirectory part of the URL (aabbcc, in this example) to become a GET parameter for that script, so a URL like the one above would be sent to example.com/script.php?id=aabbcc, while hiding this more complicated URL from the user.
I can see from other .htaccess examples that this must be possible, but I can't find one doing this.
Is there a .htaccess solution for it? Is there something else even more basic? Your help is appreciated in steering me.
If your "unique reference codes" consist of 6 lowercase letters, as in your example then you can do something like the following in your root .htaccess file using mod_rewrite:
RewriteEngine
# Internally rewrite "/abcdef" to "script.php?id=abcdef"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[a-z]{6}$ script.php?id=$0 [L]
If you don't need direct access to any subdirectories off the root that also happen to match a "unique reference code" then you can remove the preceding condition (RewriteCond directive). With the condition in place then you naturally can't access any "unique access codes" that happen to also match the name of a subdirectory.
$0 is a backreference to the entire URL-path that the RewriteRule pattern (first argument) matches against.
Reference
Apache mod-rewrite Documentation - Contents
Apache mod_rewrite Introduction
Apache mod_rewrite Reference
RewriteRule Directive
RewriteCond Directive

.htaccess rewrite /files/users/1/file.pdf to /view/?file=file.pdf

I am terrible with mod_rewrite however I need to rewrite any request to the folder /files/users/*/ (* is a wildcard) to /view/ and insert the filename into a query paramater like so:
/files/users/9/test.pdf becomes /view/?file=test.pdf
How would I go about this assuming that the .htaccess file will be located inside /files/users/?
I would really appreciate if you explained how your solution works as I am slowly trying to become familiar with mod_rewrite.
So, you wanna have all my trade secrets on a silver plate?
Well, I try my best. ;-)
First of all, you must know where the documentation is. Look here for the reference: mod_rewrite. Or mod_rewrite, if your Apache version is 2.2.
You will find an overview with lots of links at Apache mod_rewrite. There, you will find a nice introduction to rewriting URLs. Also look here for lots of standard examples.
Since mod_rewrite supports PCRE regular expressions, you might need perlre and/or regular-expression.info from time to time.
Now to your question
RewriteEngine On
RewriteRule ^(?:.+?)/(.*) /view/?file=$1
This might already be sufficient. It looks for a subdirectory (?:.+?) in /files/users and captures the name of a file (.*) in this subdirectory. If this pattern matches, it rewrites the URL to /view/?file= and appends the captured file with $1, which gives /view/?file=$1.
All untested, of course, have fun.
P.S. Additional info is here at SO at .htaccess info and .htaccess faq.
Put the directive below in your .htaccess file to rewrite /files/users/9/test.pdf to /view/?file=test.pdf. In practical terms this means that if you visit http://yourdomain.com/files/users/9/test.pdf then the visitor will be served the rewritten url which is http://yourdomain.com/view?file=test.pdf
RewriteRule ^[^/]+/(.*)$ /view/?file=$1 [L]
A RewriteRule directive is part of the Apache mod_rewrite module. It takes two arguments:
Pattern - a regular expression to match against the current URL path (note that the URL path is not the entire URL but eg. /my/path, but in a .htaccess context the leading slash / is stripped giving us my/path).
Substitution - the destination URL or path where the user will rewritten OR redirected to.
Explaining the rule
The pattern ^[^/]+/(.*)$:
^ - the regex must match from the start of the string
[^/] - match everything but forward slash
+ - repetition operator which means: match 1 or more characters
/ - matches a forward slash
(.*) - mathes any characters. The dot means match any character. The star operator means match ANY characters (0 or more). The parantheses means the match is grouped and can be used in backreferences.
$ - the regex must match until the end of the string
The substitution /view/?file=$1:
...means that we rewrite the URL path to the /view/ folder with the query parameter file. The query parameter file will contain our first grouped match from the pattern as we pass it the $1 value (which means the first match from our RewriteRule pattern).
The [L] flag:
...means that mod_rewrite will stop processing rewrite rules. This is handy to avoid unwanted behaviour and/or infinite loops.

htaccess to redirect both folder and file name

I have a file in a folder like this folder1/folder2/folder3/filename.htm. I also have another file like this folder1/folder2/folder3-filename.htm. When i browse the url mysite.com/folder1/folder2/folder3/filename.htm, i want the folder3-filename.htm will be served (displayed)
I know some basics about file name rewrite rules, but i have not seen any mixed-up combination of folder name and file name in that way.
Could anyone help me how to do it?
Using the expression [^/]+ to match all characters up to but not including the the next / and then .+ to match everything after the last /, the following will work:
RewriteEngine On
RewriteRule ^folder1/folder2/([^/]+)/(.+)$ folder1/folder2/$1-$2 [L]

Using .htaccess to style URL directory style

I have searched this question and looked around but can't seem to get this working in practice. This is my .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule /poker/(.*)/(.*)/$ /poker/?$1=$2
I am trying to get my page to work like this:
mysite.com/poker/page/home
But this just isn't working, I have used 3 different generators and tried typing it manually from tutorials but it is just returning a 404. Any idea's a greatly appreciated, it could be really obvious..
Thanks
You do not have a trailing slash in your example, yet your rule requires one. You can make the trailing slash optional:
RewriteEngine on
RewriteRule /poker/(.*)/(.*)/?$ /poker/?$1=$2
Note however, that a uri /poker/a/b/c/d/e/f/g/ is also a match here - a/b/c/d/e/f will match the first subpattern and g will match the second one, because (.*) is greedy. Be more specific if you wish to match only content between slashes - e.g. ([^/]*)
Well, there's really nothing wrong with the rules that you have if http://mysite.com/poker/?page=home resolves correctly. The only thing is that if this is in an htaccess file, the leading slash is removed from the URI when it's matched against in a RewriteRule, so you need to remove it from your regular expression (or maky it optional):
RewriteRule ^poker/(.+)/(.+)/?$ /poker/?$1=$2
And maybe make the groupings (.+) instead so that there is at least one character there.

Removing unwanted characters from URL in htaccess

Our current htaccess setup correctly converts urls like this: site.com/page.php?sid=Friend to site.com/Friend
However, due to an unrelated oversight, we had almost all of our URLs double-indexed as site.com/Friend> Because the greater than sign is a special character it doesn't call page.php so the > needs to be stripped out in htaccess and can't be done on page.php. Compounding matters is that the way they're indexed is as: site.com/Friend%3E which also might need to be stripped out.
What we would like is to have another directive that looks for an ending of > (or %3E), strips it off, then redirects to the variable that's there without that ending > In essence so that site.com/Friend> (or site.com/Friend%3E) still points to site.com/Friend
Thank you for your help.
Add this to the top of your rules:
RewriteRule ^/?(.*)>$ /$1 [L,R=301]
You can use > because the URI gets decoded when matching in a RewriteRule.

Resources