I have the following URL:
http://somedomain.com/aa/search/search.php
I want it to return 2 selections, that of "aa" and that of "search/search.php".
With the help of Regex Coach, I have made the following regular expression which targets these two just fine:
/([a-z]{2})/(.*)
However, when I use them in my htaccess file, the rewrite just doesn't happen:
Options +FollowSymlinks
RewriteEngine on
RewriteRule /([a-z]{2})/(.*) /$2?var=$1
Can someone point this regex newbie in the right direction?
edit:
By "doesn't happen", I mean that following the URL: somedomain.com/aa/search.php gives me
"/aa/search.php not found"
rather than
"/search.php?var=aa not found".
That depends on where you define the rule. Your syntax is correct for server (global) config files. If you use .htaccess files, the server will strip the path upto the place where the file is located from the URL. Try
([a-z]{2})/(.*)
(i.e. without the first slash)
Related
I have a web page that generates QR code.
I want the link to look like "website.name/qr/data_to_convert"
So I used the following in .htaccess file.
RewriteRule ^([\s\S]+)/?$ create.php?data=$1
This is so that I can convert any character set to QR code.
But I get an error.
So I tried to make it:
RewriteRule ^([A-Za-z0-9\!\#\#\$\%\^\&\*\(\)\_\-\+\=\{\}\[\]\;\:\'\"\<\,\>\.\?\|\~\`\s\/\\]+)/?$ create.php?data=$1
And now the data that is passed is "create.php"
I have just one rewrite rule in this htaccess file apart from the
RewriteEngine On
command.
What should the regexr be so that I can include any text after "qr/" to convert it to QR code.
Thanks.
Update: I get the data passed as create.php even for the first regex
I'm having this very annoying problem with my rewrite rules in the .htaccess file.
The context
So what I want is to have these two types of URLs rewrite to different targets:
URL 1 -- http://example.com/rem/call/answer/{Hex String}/{Hex String}/
URL 2 -- http://example.com/answer/{Hex String}/{Hex String}/
This is an extract of my .htaccess file:
RewriteEngine On
RewriteRule rem/call/answer/([a-f0-9]+)/([a-f0-9]+)/?$ /TARGET1
RewriteRule answer/([a-f0-9]+)/([a-f0-9]+)/?$ /TARGET2
The problem
Now the problem is that URL 2 rewrites well (using rule #2) and goes to TARGET 2, but URL 1 rewrites with both rules instead of just rule #1.
I tried several solutions, including the obvious use of the character ^ for "start of string". At that point, my rewrite rules were:
RewriteEngine On
RewriteRule rem/call/answer/([a-f0-9]+)/([a-f0-9]+)/?$ /TARGET1
RewriteRule ^answer/([a-f0-9]+)/([a-f0-9]+)/?$ /TARGET2
However, another problem happened. This time it's URL 1 that rewrites well, with only rule #1 and goes to TARGET 1. But now URL 2 doesn't rewrite at all any more. I'm guessing it's because the second rewrite rule never matches any url and thus never applies.
The only solution I found so far is to remove the ^ and use the [L] flag at the end of rule #1 like so:
RewriteEngine On
RewriteRule rem/call/answer/([a-f0-9]+)/([a-f0-9]+)/?$ /TARGET1 [L]
RewriteRule answer/([a-f0-9]+)/([a-f0-9]+)/?$ /TARGET2
This way, it uses rule #1, matches, but never gets to rule #2. Both urls get rewritten properly with these rules, but it is not a good solution since I might not want to stop the rewriting of URL 1 after the first rule applies (what if I have a third rule I would want to apply to it as well...)
My questions to you
Now that I've stated the problem, my questions here are:
Is the [L] flag the only way to go ? (which I highly doubt, and certainly hope not)
Would ^ be a candidate solution ? (I think so)
If so, how to make it work and why is it not working at all in my case ?
What I suspect
I suspect that it has something to do with the fact that the URL is actually http://example.com/answer/{Hex String}/{Hex String}/ and not just answer/{Hex String}/{Hex String}/, which means that answer/.. isn't really at the beginning of the string and thus prefixing it with ^ doesn't work.
But then it brings me to another question:
How to tell apache to strip the url of the scheme+domain part (i.e. http://example.com/) and match rules with the remainder of the url only (e.g. answer/{Hex String}/{Hex String}/) ?
EDIT
I should also add that I've tried the basic alice-bob example. I have a file named bob.html in my root and the following rule in my .htaccess file:
RewriteRule alice.html$ /bob.html
This works just fine and displays the bob.html page when alice.html is queried. However, if I change the rule to:
RewriteRule ^alice.html$ /bob.html
I then get a 404 error when querying the alice.html page...
As for #anubhava's comment, my full .htaccess file is composed as follows:
RewriteEngine On
[A bunch of RewriteRule that have nothing to do with the topic at hand
(don't contain any "answer" string in them and all work perfectly)]
RewriteRule rem/call/answer/([a-f0-9]+)/([a-f0-9]+)/?$ /TARGET1 [L]
RewriteRule answer/([a-f0-9]+)/([a-f0-9]+)/?$ /TARGET2
ErrorDocument 404 /404.html
Header set Access-Control-Allow-Origin "*"
SetEnv file_uploads On
Ok so, thanks to #anubhava's comments, I solved the problem easily by moving the .htaccess file down one level to the www directory.
I was still quite curious about why this solved my problem, so I went on investigating how apache's rewriting works. I'm not sure I've got all the details right, but here's what I found out.
Location location location
Of course, it goes without saying that the location of files is important, and especially configuration files like .htaccess. But it goes even beyond simple file path, and here is the reason why:
First, you need to keep in mind that the .htaccess file will affect the directory it's located in as well as all its subdirectories. So it would seem logical that a global .htaccess file should be placed at the root directory of your website, since it will affect all subdirectories (i.e. the whole website).
The second thing to keep in mind is that the public_html directory (which in my case was called www, simply a symbolic link to public_html) is the root folder of your website's content. You might have access to its parent directories, but whatever you put outside of your public_html directory is not part of your website's content per se, any resource you put there won't be part of your website's hierarchy (i.e not accessible via http://example.com/path/to/resource).
The regex option ^ matches the start of a string, here in the context of URL rewriting, it's the start of the considered URL. And that's not all, it seems that Apaches resolves matches relatively to the location of your .htaccess file. Which means that the ^ not only references the start of the string you wrote as part of the rule but actually references it relatively to the actual path of the .htaccess file which acts as a "local root directory" for all the rewrite rules in that specific .htaccess file.
Example
Let's say you have a subdirectory (e.g http://example.com/sub/directory/) and inside it you have two files:
http://example.com/sub/directory/.htaccess
http://example.com/sub/directory/bob.html
inside this .htaccess file, you have a rewrite rule as follows:
RewriteRule ^tom.html$ /sub/directory/bob.html
This rule will not match http://example.com/tom.html as you could expect the ^ to act, but instead will match http://example.com/sub/directory/tom.html since this is where the .htaccess file is located.
Conclusion
Generally speaking, let's say you have a rewrite rule such as:
RewriteRule ^PATH$ /TARGET_PATH
This means that the rule will not match the URL against ^PATH$, but instead it actually matches it against ^[Location of the .htaccess file]/PATH$
In other words, the location of the .htaccess file acts as a sort of base URL for all rewrite rules in it (much similar to the base tag in html).
This is why my rewrite rule with ^ didn't work, since my .htaccess file was located above the public_html directory, and that parent directory was acting as the base URL for my rules. Thus the rule would never match any URL since it would compare it with a path never accessed (because above the website's content root).
I hope this was clear enough to help anyone who might encounter the same problem I had.
Cheers
I'm trying to implement nicer urls using mod rewrite to a php script I wrote to view documents pulled from a mysql database by their entry name. i use the following code and it works fine.
RewriteRule ^view.doc.(.+) index.php?doc=$1
so basically
view.doc.xyz whould be index.php?doc=xyz
But im having trouble with entry names with '/' and '\' in them being viewed through the rewritten link.
view.doc.abc/123 into index.php?doc=abc/123
or like
view.doc.sg1\123 into index.php?doc=sg1\123
Ether i'd get an endless loop or get a 404 error. I'm pretty new to mod rewrite and Id very much appreciate any help on the matter.
Try this rule:
RewriteRule ^view\.doc\.(.+)$ index.php?doc=$1 [L,QSA,NE,NC]
I propose a slightly different solution from the #anubhava's one, by prepending a slash before index.php, tested on Debian/Apache2:
RewriteEngine On
RewriteRule ^view\.doc\.(.+)$ /index.php?doc=$1 [L,QSA,NE,NC]
It gives:
view.doc.abc/123 => /index.php?doc=abc/123
I cannot test anti-slash \, as no browser permits me to test this in URL without transforming it to slash /.
Recently, I'm experimenting with PHP's mod_rewrite engine. A bunch of tutorials I've read gave me a pretty good picture how to use its most basic and useful possibilities. But there is still that question I didn't find the answer for. I guess it should be the very first question to be explained but no tutorial gave me the answer yet.
I'm wondering which very part of URL is being considered when trying to match the regex.
Let's say I have a directory my_project on my server and a .htaccess file inside that directory. The browser should see the directory like this:
http://my_website.com/my_project
If I add a rule in .htaccess then which part of the above URL will be considered when trying to match the regex of this rule? I'm pretty good in understanding regular expressions themselves but I can't figure out which chunk of URL does mod_rewrite pick to do the regex.
If my question isn't clear enough let me also put it this way: which exact place of the above URL is matched by the following regex in .htaccess?
^
Yet another question, if I go to
http://my_website.com/my_project/subfolder
will the considered part of the URL will be different or it will always depend on the place where .htaccess is placed?
I figured it out.
To explain the problem and how I got to the answer I'll try to explain it step by step.
Let's assume the following:
.htaccess is placed in a folder my_project in the root path of www.my_website.com. .htaccess consists the following rule:
RewriteRule ^.*$ index.php?matched=$0
To avoid endless loop let's "fire" the rule only if we provide a test parameter in query string, so the complete .htaccess should look like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} test=1
RewriteRule ^.*$ index.php?matched=$0
Now, if everything goes as I thought we should end up in the index.php script placed in my_project folder. To see the whole match let's add the following line to the script:
var_dump($_GET["matched"]);
In the browser we go to http://my_website.com/my_project?test=1 and we expect the output to be:
string(32) "http://my_website.com/my_project"
But it is not! It is instead
string(0) ""
We're almost there. Now let's go to http://my_website.com/my_project/subfolder/?test=1. The output is
string(10) "subfolder/"
That proves one thing - when mod_rewrite starts to compare the URL with regular expressions it doesn't see the PROTOCOL part of the URL as well as the HTTP_HOST part. As my further research reveal, it also ommits every folder above the .htaccess location as well as the query string and hash part of the URL. For the mod_rewrite the URL begins where the .htaccess location begins.
I hope this self-answered question will be helpful for someone in the future.
Enjoy!
Let me give you a practical example
Suppose Your website is www.example.com and it's located in a folder/Directory named 'ex'
You'll place the .htaccess file in your ex folder to make it work for your website www.example.com
Now let's say you want to make this url clean www.example.com/ex/index.php?page=welcome
open your.htaccess file that you have placed in your ex folder and add this following code to it
RewriteEngine On
RewriteRule ^([A-Za-z0-9-+_%*?]+)/?$ index.php?page=$1 [L]
It'll chagle the URL from www.example.com/ex/index.php?page=welcome to www.example.com/ex/welcome
Now let's say you moved your website to a subfolder ex/subfolder OR www.example.com/ex to www.example.com/ex/subfolder
Simply move the .htaccess file with all of your site to that subfolder no need to change the code it'll work the same
([A-Za-z0-9-+_%*?]+) <-- this part with in the brackets is used as regular expression
means you are looking for any Character from A to z and from a to z and any number from 0 to 9 and symbol - , +,_,%,*,? and the + sign after the closing square bracket means more than one .
In short You are asking to for which is ([in here]+) and it's more than one ,if however you remove the + symbol after the bracket it'll return only the first character
If the rule:
RewriteRule myurl /newurl
Matches:
http://localhost:8080/test/xxxmyurlyyy
Why doesn't it match this?
http://localhost:8080/test/xxxmyurly%EDyy
EDIT:
I've just discovered utf-8 encoded works just fine. If I rather use %C3%AD than %ED it's ok. I still need to enable unicode.
It seems to match just fine. Given this:
RewriteEngine On
RewriteLog /tmp/rewrite.log
RewriteLogLevel 5
RewriteRule myurl /stackexchange/foo.html
If I fetch this:
curl http://localhost/test/xxmyurly%EDyy
I see this in /tmp/rewrite.log:
(3) applying pattern 'myurl' to uri '/test/xxxmyurly?yy'
(2) rewrite '/test/xxxmyurly?yy' -> '/serverfault/foo.html'
And I get exactly what I expect (a document with the content "this is a test").
I suspect your problem is other than you think it is. Enable RewriteLog and
see what shows up, and then post your results here. Also, seeing more of
(ideally, all of) your configuration would help, too.