.htaccess excluding directory issue - apache

I have a website consisting of a single index.html file. I have several menus, and need to go 3 levels deep, so I want a php-like structure as such: index.html?main=$1&secondary=$2&tertiary=&3. This has to be reduced to www.example.com/$1/$2/$3. Overall pretty simple I would think, using following rules for in .htaccess:
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)?$ index.html?main=$1&secondary=$2&tertiary=$3
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)?$ index.html?main=$1&secondary=$2
RewriteRule ^([A-Za-z0-9-]+)?$ index.html?main=$1
Now, I also have several folders in my root folder that shouldn't be affected, otherwise my include's wont work. Looking at this answer I've already tried exluding these folders using RewriteRule ^(bower_components|photos)($|/) - [L] before the other rules, but it didn't work. I've also tried this answer, making a .htaccess with contens RewriteEngine Off and putting it in my folders, also without success. So obviously I'm doing something wrong somewhere. To show my folder layout, here's a quick snapshot of it:
Here's my current .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)?$ index.html?main=$1&secondary=$2&tertiary=$3
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)?$ index.html?main=$1&secondary=$2
RewriteRule ^([A-Za-z0-9-]+)?$ index.html?main=$1
Now, if I go to http://localhost/myProject/activities, so 1 level deep as the index.html file is located in myProject, it does work and all includes are included correctly. However, when going to http://localhost/myProject/activities/test, I get to the basic index.html page, but my includes point to http://localhost/myProject/activities/bower_components/platform/platform.js, so the activities is too much.

Keep your rules like this:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /myProject/
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.html?main=$1&secondary=$2&tertiary=$3 [L,QSA]
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/?$ index.html?main=$1&secondary=$2 [L,QSA]
RewriteRule ^([A-Za-z0-9-]+)/?$ index.html?main=$1 [L,QSA]
Then for css/js/image inclusion just use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.
You can also try adding this in your page's HTML header: <base href="/myProject/" /> so that every relative URL is resolved from that URL and not the current URL.

Related

Change root directory in .htaccess

I have a problem with changing the root directory in .htaccess.
My folder structure looks like this.
What I want to achieve is, when I visit this page:
/comparty/about/
The page I will see is this page:
/comparty/pages/about/
I have already tried to search on Google, but the code I found did not work, though I tried to change it:
RewriteEngine On
RewriteBase /comparty/
RewriteRule ^(.*)$ pages/$1 [L]
I don't want it to redirect, I want to keep the same URL. Also I've had a big problem with Apache caching the .htaccess file, so I haven't been able to test many things.
Thanks in advance.
EDIT
I found a way to rewrite the URL from /comparty/pages/about/ to /comparty/about/ - this is the code:
RewriteEngine On
RewriteBase /comparty/
RewriteRule ^about/(.*)$ pages/$1 [L]
This only works on the about page, though. What would I have to do, to make it dynamic and work with every page?
You need to use a dynmic pattern :
RewriteEngine On
RewriteBase /comparty/
#if the request is not for an existent dir
RewriteCond %{REQUEST_FILENAME} !-d
#and the request is not for an existent file
RewriteCond %{REQUEST_FILENAME} !-f
#rewrite the request to "/pages/request"
RewriteRule ^(.*)$ pages/$1 [L]
RewriteConditions above are important to avoid rewriting your existent files and directories to the /pages subfolder. Without those conditionrt the Rule will rewrite all requests including the destination path /pages and this may result in rewrite loop error.

mod_rewrite directory heirarchy, rules specific to rewritten URLs?

I have directories and subdirectories similar to below:
Each file and folder here is preceded by localhost/dir/
/common/style.css
/common/js.js
/common/top.php
/common/bottom.php
/images/items/1.png
/images/items/2.png
/images/items/3.png
/index.php
/items.php
/specificitem.php
On every page in the default directory, everything in the common folder is included (each page includes top.php and bottom.php, top.php includes js.js and style.css). specificitem.php relies on $_GET to grab data to populate that page, and is directed to from items.php.
In my .htaccess file, I rewrite the URLs for specificitem.php like the below:
RewriteEngine on
RewriteBase /dir/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule items/john specificitem.php?id=1 [NC,L]
RewriteRule items/jane specificitem.php?id=2 [NC,L]
RewriteRule items/jill specificitem.php?id=3 [NC,L]
...
When directing to these pages, no links work, styling and Javascript are broken/not loaded. Links are in the form ./somelink.php, and when on one of the item pages (eg. localhost/dir/items/john), ./somelink.php tries to direct to localhost/dir/items/somelink.php when this page doesn't exist. Stylesheets and Javascript are also attempted to be found at localhost/dir/items/common/ when likewise this doesn't exist. Changing the links to /somelink.php instead tries to go to /localhost/somelink.php when this doesn't exist.
What have I done wrong in my .htaccess file, or what is missing such that I can apply link rules to rewritten links only? My links, extra files, images are loaded properly for any page without a rewritten URL.
First have your rules like this:
RewriteEngine on
RewriteBase /dir/
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^items/john/?$ specificitem.php?id=1 [NC,L,QSA]
RewriteRule ^items/jane/?$ specificitem.php?id=2 [NC,L,QSA]
RewriteRule ^items/jill/?$ specificitem.php?id=3 [NC,L,QSA]
Then to fix links/css/js/images paths you need to add this just below <head> tag of your page's HTML:
<base href="/dir/" />
so that every relative URL is resolved from that base URL and not from the current page's URL.

Apache mod_rewrite file in main directory to be in fake directory

I have all of my website files in one directory and am trying to create a nested linking appearance. The code I have only grabs the first rule and applies it, but I'd like to specify where each URL gets redirected to. Right now both of these rules pull up how-we-invest.php.
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?how-it-works(/.*)?$ how-we-invest.php [L,NC]
RewriteRule ^/?how-it-works(/.*)?$ pricing.php [L,NC]
In my head it seems I should be able to do something like this, but it doesn't work:
RewriteRule ^/?how-it-works/how-we-invest/?$ how-we-invest.php [L,NC]
So, ideally when you go to website.com/how-it-works/how-we-invest/ it would pull in how-we-invest.php which sits in the root directory.
Thanks for your help.

Redirecting via .htaccess to .php with arguments in current folder

I'm trying to redirect something like foo/bar to ?foo=bar, so I can do www.mydomain.com/hey/foo/bar to www.mydomain.com/hey/?foo=bar, but I can't seem to get the syntax right. I tried the following:
RewriteEngine on
RewriteRule ^foo/(.*)$ ?foo=bar [NC]
But this doesn't work. How would I accomplish this? I tried adding a forward slash behind the question mark, but that makes it link to the root directory.
Thanks,
Jengerer
You need to include the actual file you're rewriting to. Using ?foo=bar isn't pointing at any file in particular.
Use the following rule:
RewriteEngine on
RewriteRule ^(.*)/(.*)$ index.php?$1=$2 [NC]
Notice that I'm pointing to the file index.php. $1 is replaced by whatever is matched by the first (.*) and $2 by the second. So, if someone browsed to foo/bar, they would be taken to index.php?foo=bar.
Important Note: If you choose to use (.*) and accept any character for the variable name or value and plan to use this information in database queries, you'll want to be certain to escape this content properly using your database's escape functions (mysql_real_escape_string or pg_escape_string) or by using prepared statements.
If you're having problems with style or elements on the page not showing correct because you're using relative paths, you'll need to use absolute paths starting at the root. Otherwise, your pages style, images, etc. would break.
<link rel="stylesheet" type="text/css" href="/path/to/style.css" />
Maybe try removing the ^ from ^foo/(.*)$?
Well, it turns out that the problem wasn't in the redirection, but in the links that were made after the redirection.
After redirecting from /foo/bar to /?foo=bar, the CSS links and images were poorly matched because it was now looking for local links within the /foo/bar directory, which doesn't exist. What an interesting 'glitch'.
When you place the .htaccess in your root it should be like this:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\/]+)\/([^\/]+)\/([^\/]+)$ $1?$2=$3 [L]
when you place the .htaccess in your "hey" folder, it should be like this:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\/]+)\/([^\/]+)$ ?$1=$2 [L]

Use symfony 1.4 without changing apache configuration

Is it possible to set the /web directory as webroot without changing apache configuration file?
I tried using the following .htaccess code, but if i go to localhost/module/, it displays 404 error. But if i go to localhost/web/module/ then everything works.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule sf/(.*) lib/vendor/symfony/data/web/sf/$1 [L]
RewriteRule ^$ web/ [L]
RewriteRule (.*) web/$1 [L]
</IfModule>
i do like this on the root :
RewriteEngine On
RewriteBase /
RewriteRule (.*) ./web/$1 [L]
And edit web/.htaccess uncommented the 'RewriteBase /' line.
this make all the mysite.com/aaaa/bbbb works like mysite.com/web/aaaa/bbbb
Short answer: no.
Bit longer: you will have to edit the apache config at least to give it permission to access the web/ directory, so even if you symlink your web folder to /var/www, it will not work.
This is quiet similar to my question Symfony on virtual host (document root problem).
This is my .htaccess in the project root directory:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/images/ [OR]
RewriteCond %{REQUEST_URI} ^/js/ [OR]
RewriteCond %{REQUEST_URI} ^/css/
RewriteRule ^(.*)$ /web/$1 [L]
RewriteCond %{REQUEST_URI} !^/web/
RewriteRule ^(.*)$ /web/index.php [QSA,L]
This solved my problem but symfony tries to generate every url (eg. using url_for) from the document root so instead of url like domain.com/my-article it generates domain.com/web/my-article.
I had to slightly modify my PatternRouting class to trim the /web prefix from each url. I think this is not the best solution but it works.
Also if I want to access backend application I have to call always /web/backend.php/ because I don't want to have so many rewrite rules in the .htaccess.
If you want to see my extended PatternRouting class source code I'll paste it here.
Yes, it is possible. Copy everything from web/ up a level to your document root. Edit index.php to reflect the fact that everything it includes is now one level closer to its current directory than it used to be (one less ../). You won't have to edit a single other Symfony file.