Having Trouble with RewriteRule syntax and configuration, challenging - apache

I'm really struggling with the syntax of a Rewrite Rule. I'm hoping you can help.
I used a node package module to create a bunch of html snapshots of a super ajaxy site. The module outputs the snapshots in a diretory named as the URL for later use with escape_fragment rewrite rules. So exampledomain.net/a45.php?lang=eng is output as ./snapshots/a45.php?lang=eng/index.html. So to cd into that directory I have to of course escape the special characters.
I am attempting to Rewrite http://testdomain.net/a45.php?lang=eng to http://testdomain.net/snapshots/a45.php?lang=eng/index.html and I cannot get my rewrite rules to work.
Here's the syntax I've tried:
in an .htaccess file in the subdirectory
Options +FollowSymLinks
RewriteEngine On
ReWriteBase /var/www/vhosts/testdomain.net/httpdocs/secure/clients/amg/3dot0
RewriteRule ^a45\.php?lang=eng$ /snapshots/a45.php?lang=eng/index.html
I have also tried it with and without RewriteBase.
I've also tried the same as above with the second url escaped and with a couple different characters on the first url ALL to no avail...
OH and I was sure to enable Allowoverride all on the entire httpdocs directory as well in the main vhost.conf file -- I know that directive is working becuase Options +Indexes allows me to browse directories throughout this tree.
Any advice or clues are appreciated greatly!

Directory names including characters ? = / are not going to be liked. Try to avoid them.
The purpose of a rewrite is to run another page from somewhere else, not dump something into a new directory. Is that what you're trying to do? You're telling the server than there is an index.html to run, in /snapshots/a45.php?lang=eng/ directory.
A RewriteRule isn't going to see the Query String anyway (?lang=eng). The ? is telling it that the p is optional.

Related

Apache - mod_rewrite how to prefer files instead of directories (if both have same name)?

I have very similar problem like this one: Apache mod_rewrite - prefer files over directories with pretty URLs
However it is not same, and solutions mentioned in above link doesn't work for me.
My directory structure looks like this:
/pages/articles/january.php
/pages/articles.php
/pages/home.php
/articles/
/index.php
Now, I am including in index.php pages (depending on url).
For example, when user types address www.domain.com (or www.domain.com/home), index.php will include /pages/home.php
But if I enter this URL: www.domain.com/articles it will make link something like this: www.domain.com/articles/?page[]=articles (in other words index.php won't include /pages/articles.php file)
On the other hand, this works perfectly: www.domain.com/articles/january
This is my htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9+]*)/([a-zA-Z0-9+]*)$ %{DOCUMENT_ROOT}/index.php?page[]=$1&page[]=$2 [QSA,L]
RewriteRule ^([a-zA-Z0-9+]*)$ %{DOCUMENT_ROOT}/index.php?page[]=$1 [QSA,L]
I use array for page because I can have subpages (and subpages work fine!).
EDIT: I have found this however it doesn't solves my problem :(
Can someone tell me why it does this and how to fix it?
Or, how can I give priority to files instead directories?
EDIT 2: I solved it by removing "/articles/" directory, however I am still interested how to make it work through htaccess file rules.
Per the comments above, your original issue was worked around by removing the articles directory, but you still wanted to be able to deal with directories in that sort of situation.
You'd probably want to split into two sets of rules. Have an earlier rule that uses the -d flag in a RewriteCond to catch directories so that it could treat them differently as needed. Alternately, ignore directories in the first rule by negating the flag (!-d) then catch them in the later rule.

Changing a file's URL without physically moving it

I have a site, running Linux + Apache.
I have a file in my root directory, let's say file.php.
I want the URL to the file to be "domain.com/newdir/file.php", but I don't want to actually create the newdir and move the file there because it would be a huge hassle to update many many links all over my site.
Is there a way to accomplish this, meaning making the file accessible by the new URL without moving it?
Thank you.
On this site: workwith.me, you can find information about .htaccess and mod_rewrite. For your example you have to make a file called .htaccess and put it in the root directory. The file should contain these directives:
RewriteEngine on
RewriteRule ^newdir/file.php$ /file.php [L]
You can do this for every file you want to rename.
Four possible solutions I can think of:
If your OS supports it, create a symlink:
mkdir /home/foo/htdocs/newdir
ln -s /home/foo/htdocs/file.php home/foo/htdocs/newdir/file.php
... and make sure Apache is configured to follow them:
Options FollowSymLinks
Create an Alias or AliasMatch (probably overkill)
Good old mod_rewrite:
RewriteEngine One
RewriteRule ^newdir/file\.php$ file.php [L]
Ugly: use a custom 404 error page with a PHP script that checks $_SERVER['REQUEST_URI'].
I guess the standard solutions are #1 and #3.

Using .htaccess rewrite rules to reflect a "fake" directory structure in the addres bar

I'm working with an online encyclopedia and I am trying to achieve the following:
Given the physical location of a file in http://example.com/articles/c/a/t/Cat.html,
Get the location in the address bar to show http://example.com/encyclopedia/Cat.html
This also needs to work so that if a link is clicked or someone types in "example.com/encyclopedia/Cat.html", the server will look for the file in "/articles/c/a/t/Cat.html", yet still serve the shorter URI in the address bar.
I understand this may involve some heavy .htaccess voodoo to accomplish, or perhaps that it would be better to use a PHP script to serve this purpose.
So far I have the following in my .htaccess:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^encyclopedia/(.*)\.html$ articles/$1.html [NC]
RewriteCond %{THE_REQUEST} ^GET\ articles/(.*)
RewriteRule ^articles/(.*) /encyclopedia/$1 [L,R=301]
</IfModule>
However with this code, it only works by going to "example.com/encyclopedia/c/a/t/Cat.html" and showing the proper page, and when you go to "/articles/c/a/t/Cat.html it still doesn't rewrite it as "/encyclopedia/", it just stays the same.
Edit - By removing the GET\ part from the RewriteCond and removing the leading forward-slash from /encyclopedia/$1 in the following line, any requests to "/articles/c/a/t/Cat.html" are correctly redirected to "/encyclopedia/c/a/t/Cat.html". I am still at a loss trying to remove the "/c/a/t" part though. **
I've tried using the following two rules to remove the "c/a/t/" part:
RewriteRule ^encyclopedia/((.)(.)(.).*)\.html$ articles/$2/$3/$4/$1.html [NC]
RewriteRule ^articles/(.)/(.)/(.)/(.*) /encyclopedia/$4 [L,R=301]
But with no success as I'm sure what's happening is I'm getting the capital "C" from "Cat.html" and putting that in as "/articles/C/a/t/Cat.html" which will obviously not work.
I've been looking around studying .htaccess RewriteRule and RewriteCond for days but I still haven't been able to figure this out and been BHOK enough to cause a few migraines.
Would this be better accomplished using a PHP script? Or can this voodoo be easily enough accomplished via only .htaccess rules?
First thing, forget about .htaccess files. .htaccess files is just an extension of Apache configuration files that you can put in some directories. They're really slowing down your apache server, he needs to check part of his configuration at runtime. It's done to allow some configuration on hosted environments.
Put everything you have in .htaccess files in <Directory> sections on your VirtualHost and use AllowOverride None to tell Apache to forget about trying to read .htaccess files.
So what you need is mod-rewrite voodoo, not .htaccess voodoo :-)
Now your rewrite problem is quite complex. If you need some mod-rewrite help do not forget to read this ServFault article : Everything You Ever Wanted to Know about Mod_Rewrite Rules but Were Afraid to Ask?
I assume that your Cat.html -> c/a/t/Cat.html is just an example and that you can have more than 3 letters : CatAndDogs.html -> c/a/t/a/n/d/d/o/g/s/CatAndDogs.html.
The part of mod-reqrite you need is (I think) RewriteMap. There you will find some helpers like lowercase: that coudl help you, but you will also find the prg: which means using an external program to perform the mapping. I would use perl examples of such rewriteMaps examples available via google and make some transformations. Should be quite easy and Fast in Perl to transform CatAndDogs.html in c/a/t/a/n/d/d/o/g/s/CatAndDogs.html.
Note that RewriteMap will never work inside a .htaccess. Forget .htaccess files. The prg: keyword will launch your perl program as a parallel daemon and will feed him with quite a lot of data, you shoudl really write something robust & fast. Do not forget to use the RewriteLock directive to avoid mixing results (some prg: mappers do not care about mixing results, think about load balancers for examples, but you do want to avoid mixing results for parallel queries)

rewritemap for SEO and pretty URLs

I am attempting to redirect & rewrite some dynamic PHP URL's to pretty and SEO friendly URLs. I have manged to do this successfully through .htaccess with the following code:
RewriteCond %{QUERY_STRING} ^somevar=green&nodescription=([a-zA-Z0-9_-]*)$
RewriteRule (.*) /green\/%1\/? [L,R=301]
RewriteRule ^green/([^/]*)/$ /script.php?somevar=green&nodescription=$1&rewrite=on [L]
This creates a somewhat pretty URL as follows:
http://www.mysite.com/green/aA43-/
As I say, this works absolutley fine. Apart from one thing. The parameter nodescription contains a non-descriptive random set of letters, numbers and other characters.
I would like to rewrite the nodescription parameter to a more descriptive one. I understand that I can do this with a rewritemap through Apache. However, I have no experience at doing soemthing like this, and I'm not entirely sure where to start.
Normally I would simply alter script.php so that it contains more descriptive parameters, but this time I have no control over the script; I am pulling it from another site using cURL.
Can anybody give me an example of how to pull this off?
Thanks!
Matt
Well, to answer my own question, to pull this off you need access httpd.conf file on your apache server. My shared hosting company didn't allow access to this file (I doubt any would allow you access).
So I bit the bullet and purchased a VPS. I will post the steps I took here in order to set the rewritemap up in the hope that it will help a lost soul :) Ok, here goes...
My VPS has WHM installed, so in WHM I went to:
Server Configuration >> Apache Configuration >> Include Editor
Pre Virtual Host Include >> All Versions
This feature takes any text you put in and includes it in your httpd.conf file without worrying that it will be overwritten at a later stage. If you don't have WHM on your server then you can add the text directly to your httpd.conf file; make sure it is outside and before any virtual hosts.
OK, so I included the following map declaration and rewrite rule:
#Map to redirect (swaps key and value)
RewriteMap rwmap txt:/home/*/public_html/rdmap.txt
<Directory /home/*/public_html/test>
Options All -Indexes
RewriteEngine on
RewriteRule ^url/([^/]*)/$ /script.php?foo=${rwmap:$1|$1}&rewrite=on [L]
</Directory>
The actual map is a simple text file containing key/value pairs - you need to place this file in the directory declared in RewriteMap rwmap txt:/home/*/public_html/rdmap.txt.
And there you go. Apache now rewrites my URLs for me and I now have some nice and pretty SEO optimized links thanks to my rewrite map! Hoorah!
RewriteEngine on
RewriteRule ^green/([^/]*)/(.*)$ /script.php?somevar=green&nodescription=$1&rewrite=on [L]
This rewrite will allow you to pass "arbitrary text" that has nothing to do with the rewrite. For example:
http://www.mysite.com/green/aA43-/some-seo-boosting-title
Will still reroute correctly to script.php; the latter part will simply be ignored by the rewrite.

htaccess rewrite question

I want to rewrite the url http://mydomain/myapp/fakefolder to http://mydomain/myapp/index.php
I tried the following rule but thats not working
RewriteEngine On
RewriteRule ^fakefolder$ index.php
The .htaccess file is located inside myapp.
Kindly help
Thanks
vineet
To begin with, your folder is not called vineetstore: it's called fakefolder.
The corrected rule works for me so I'd dare say your Apache installation is not configured to read .htaccess files in such location. You can easily test that: make a syntax error on purpose and see whether your site crashes.
Find your virtual host or site definition and make sure you have this directive:
AllowOverride All