Relative file path issue while using Apache rewrite rules - .htaccess

I am trying to create some Apache rewrite rules that shall be able to route URI requests like e.g.
http://mydomain.com/articles/example to
http://mydomain.com/index.php?site=articles&page=example
What I've got so far is this:
RewriteRule ^/?([a-zA-Z_]+)/([a-zA-Z0-9_]+).html$ index.php?site=$1&page=$2 [L]
RewriteRule ^/?([a-zA-Z_]+)/([a-zA-Z0-9_]+)$ index.php?site=$1&page=$2 [L]
Since I am using relative file paths everywhere in my index.php, for style sheets, scripts, images and the like, I am facing the problem of incorrectly resolved absolute file paths.
For example when I am in the root, http://mydomain.com/ or http://mydomain.com/articles, all relative image paths resolve correctly - images/logo.jpg becomes http://mydomain.com/images/logo.jpg or htdocs/mydomain/images/logo.jpg respectively.
When I click one of my links on the site, e.g. http://mydomain.com/articles/example, Apache (or the Browser?) assumes all my images are located here http://mydomain.com/articles/images/ - it is pretty obvious that this path doesn't really exist.
So here are my questions:
1. Is it possible to solve this problem without changing all my relative file paths to absolute ones?
I could use root / for all my paths on my actual web server, because my domain is advantageously linked.
mydomain.com is linked to /html/mydomain on my web server - / resolves to mydomain.com/, but on my personal computer I am running XAMPP and / resolves to htdocs/ - it should however resolve to htdocs/mydomain/ simply because I hold several sites in htdocs
Absolute file paths are terrible when updating index.php to my web server, because I have to auto-systematically replace path declarations - not only in index.php, but style sheets and scripts as well.
2. Should I create a static DNS entry on my personal computer to adapt to my web server?
3. What is the most commonly used method?
Thank you in advance!
Sincerely, Sebastian

I figured it out by myself, by editing Windows' hosts file and the Apache's httpd-vhosts.conf file.
1. Adding a new static DNS entry to %SystemRoot%\system32\drivers\etc\hosts
127.0.0.1 www.mydomain.local
2. Appending a new virtual host entry to %ApacheInstallDir%\conf\extra\httpd-vhosts.conf
<Directory "E:\htdocs">
Order Deny,Allow
Allow from all
</Directory>
NameVirtualHost 127.0.0.1
<VirtualHost 127.0.0.1>
DocumentRoot "E:\htdocs"
ServerName localhost
</VirtualHost>
<VirtualHost 127.0.0.1>
DocumentRoot "E:\htdocs\mydomain"
ServerName www.mydomain.local
</VirtualHost>
Now http://www.mydomain.local leads to htdocs/mydomain directly
3. Changing all relative paths and links in my index.php by simply "rooting" them with a / in front of them, e.g. <img src="/images/logo.jpg" />
I also managed to set up a Site in Dreamweaver, so I am able to use the Design- and the Live-View correctly.

Related

Configure httpd.conf to add new site on Apache Red Hat Linux

I have linux server (Red Hat Enterprise Linux Server release 5.3 (Tikanga)) with apache installed. It is already used for browsing some documents. Now I would like to add a new Directory (with a html page), so whenever the directory is browsed it can display the html page.
But I am not sure of where all to edit the httpd.conf file
Existing httpd.conf:
When I hit the url "http://servername/eng" it displays list of folders.
Now, I want to add a website to this existing, so when user hit the url "http://servername/builds" it should display a html page in the browser.I have added my "index.html" page in location "/var/www/html/builds/"
For this I added the below code to httpd.conf file
Please let me know what all modifications are required in the conf file
You can do it in a few different ways.
Putting index.html in /build
This requires you to have this setting:
DirectoryIndex index.html
(it should be there by default on most platform.)
Also for this to work, rather than putting new <Directory>, you should put the build/ directory in the directory that holds your http://example.com/ files. For instance:
/var/www/example.com/public_html/eng/
/var/www/example.com/public_html/builds/
/var/www/example.com/public_html/builds/index.html
Storing build/ in folder completely unrelated to example.com, but still be able to reach it via example.com/builds
For this, you need to rewrite the URLs so that example.com/builds redirects the user to the final URL. This is most easily achieved through mod_rewrite. You enable mod_rewrite module in your Apache's configuration, make sure that example.com can have .htaccess files through ensuring proper AllowOverride entry in example.com's <Directory> configuration, create /var/www//example.com/public_html/.htaccess (or similar) file, and fill it RewriteEngine On and RewriteRules you need. More on mod_rewrite in the Internet and in the documentation.
Completely separate virtual server, for example builds.example.com/
In this case, what you're looking for are virtual servers. These are not defined in httpd.conf or configuration itself, but usually have dedicated directory.
For example, to add builds.example.com that works for port 80, you'd need to create following entry:
<VirtualHost *:80>
ServerName builds.example.com
DocumentRoot /var/www/builds.example.com/public_html/
</VirtualHost>
Where to put this? Well, it depends on the platform. For Debian, you put this in a new file in /etc/apache2/sites-available/, e.g. /etc/apache2/sites-available/example.com, and symlink to it in /etc/apache2/sites-available (on Debian, you can do this easily with a2ensite <NAME_OF_FILE>. On your platform this procedure might be different, so look it up ("adding virtual servers on " would be a start). After adding virtual servers, you need to reload your Apache configuration.
Please let me know if this satisfies your question, if not, I'll edit the answer accordingly.

.htaccess not working on xampp Ubuntu 14.04

I have difficulties making my .htacces work on my Ubuntu 14.04 LTS. I know it is a hidden file and all; I've searched everywhere and people seem to have the same problem. I've made a virtual host on my machine and all. Can someone please help me ?
ErrorDocument 404 /pages/error.php?code=404
ErrorDocument 403 /pages/error.php?code=403
You need to make sure the following things are true:
You need to check what DocumentRoot is set to. When an url beginning with a slash is detected, it will try to load that file relative to the document root. (docs) The file <documentroot>/pages/error.php must thus exist.
You need to make sure that .htaccess files are allowed by the main Apache configuration. The easiest way to test this is by entering garbage into your .htaccess file, saving it and reloading a page. You'll get an internal server error if Apache reads the .htaccess file. Otherwise the page loads as expected. To enable .htaccess files, the AllowOverride directive must allow something (see next bullet point). Additionally, check in httpd.conf if AccessFileName is set to something different than .htaccess. Change it as necessary, then RESTART APACHE. (docs)
For ErrorDocument, AllowOverride must be at least set to FileInfo. See the docs. Go to your main config file (httpd.conf), probably apache/Apachex.y.z/conf/httpd.conf. Search for the <Directory ...> block that corresponds to your http root, and look around if it contains an AllowOverride directive. Add FileInfo as one of the arguments, save the file and RESTART APACHE.

How Change Subdomain Root Folder to Public_html via .htaccess?

I have a subdomain that I want to make the root folder to "Public_html",
for example, my root subdomain is
- /public_html/subdomain
So I want to change that path to just /public_html/ for my subdomain. I know it can be done with .htaccess, but I dont know how to do it. Thanks...
I don't know exactly what you want, but here are my two answers:
Answer 1: You want your DocumentRoot to point to public_html
In this case add/change the DocumentRoot in your (virtual) host config of your webserver (I'm assuming you're using Apache, which would be /etc/httpd/conf/httpd.conf or /etc/httpd/conf/extra/httpd-vhosts.conf or even /etc/apache2/sites-enabled/yourhost)
Answer 2: You want to redirect from your subdomain folder to the root of public_html
This is not possible via a simple RewriteRule due to the fact, that your host is pointing to the subdomain folder. The other way round would possible (redirecting from public_html to subdomain).
What you can to is to create a symbolic link from subdomain which points to public_html or a file in it - but I don't recommend this.
Furthermore resources:
A brief description on how to forward to a subfolder using Rewrite rules
Another brief description about the DocumentRoot

htaccess file not working with wamp

I'm running WAMP on my Win7 box and trying to configure an htaccess folder to restrict my guests from browsing the directory structure.
Right now, if you head to http://grovertechnologysolutions.com/Technical, it will list the directory structure which I do not want. They should be only able to get to the .html site.
I've already modified the httpd.conf file to remove the commented out rewrite mod.
I've added the htaccess file to the documentroot of my website.
My htaccess file looks like this:
options -indexes
# or #
IndexIgnore *
I've restarted all services multiple times.
Should the htaccess be in the WWW folder or the document root? I run many websites virtually and only care about my main site, so I've been placing the htaccess inside that folder, but have tried www as well.
I'm assuming you've edited the proper .htaccess file in the website's DocumentRoot folder.
Options -Indexes
The above will only work if your particular WAMP's configuration and the website's VirtualHost file has the proper AllowOverride value set, such as...
AllowOverride All
Check the website's VirtualHost file, and/or httpd.conf's for something like...
<Directory "C:/WampDeveloper/Websites/www.example.com/webroot">
Options All
AllowOverride All
order allow,deny
allow from all
</Directory>
If it's not set to All nor lists Indexes, that .htaccess line is going to get ignored.
I've already modified the httpd.conf file to remove the commented out rewrite mod
mod_rewrite has nothing to do with this.
I've restarted all services multiple times.
.htaccess files are re-read on every request. You don't need to restart Apache.
http://httpd.apache.org/docs/2.2/mod/core.html#allowoverride

make all subdomains access website root files

Here's what I want to do:
Let's say i have www.mysite.com and it's a complex website with alot of files
I want to make fr.mysite.com, it.mysite.com, uk.mysite.com to access the file in the root website (ie: fr.mysite.com/jobs.php will actually load mysite.com/jobs.php but in the browser url it will show the link with subdomain).
I want to build a site with content from multiple countries and I want each country to be accessed with it's code as a subdomain. If I can do that I can then process the url in php and know what country code it's in the url.
Copying the entire site to each subdomain folder isn't an option.
Let me know if you have any idea on how to do that, I guess it's a .htaccess thingy but I can't figure it out .
Use ServerAlias in your VistualHost configuration: set it up as single virtual host where all sub-domains point into the same root folder:
<VirtualHost *:80>
ServerName mysite.com
ServerAlias *.mysite.com
DocumentRoot "\path\to\your\site\"
...
</VirtualHost>
This is the most recommended way -- no need to involve URL rewrite here.

Resources