mod_rewrite multiple fake subdirectories - apache

Sorry if this is a really dumb question - I can't find the answer anywhere...
I'm storing my test files in a subdirectory called 'dev', and using the following rewrite rule so that requests to /dev/VAR1/VAR2 will take me to /index.php?page=VAR1/VAR2, and requests to /dev/VAR1/ will take me to /index.php?page=VAR1
(VAR1 and VAR2 are variables, and the folders/subdirectories won't really exist)
RewriteBase /dev
RewriteRule ^([A-Za-z/\-]+)/$ index.php?page=$1
This is doing it fine - my index.php is getting the right variables. The problem is that the server or page (or I don't understand what...) thinks I'm actually in the non-existent folder (or subdirectory) which is VAR1, so all my relative (if that's the right word) addresses for images and links and css and scripts etc
<link rel="stylesheet" type="text/css" href="./sitefiles/mystyles.css" />
all work as if they were /dev/VAR1/sitefiles/mystyles.css - basically, the server thinks it's actually in the 'fake' folder.
Is there a way I can continue to use these relative addresses with my mod_rewrite, or do I have to use 'absolute' addresses with the complete path?
Thanks for any help!

We're using the same stuff with our configuration and rewriting fake aliases to index.php?something=$1 we have to put our CSS files into the /styles/ of the DocumentRoot and don't use relative ./ path use the absolute path like href="/sitefiles/mystyles.css" it should work fine.

Related

htaccess RewriteRule problems

I have a web page which works fine on live server. However some links to files (jpg, pdf and others) which are created with cms editor contain relative paths.
When I run that page on my local test server which serves the pages out of a sub folder of localhost the relative paths to the files are wrong since they are missing the subfolder at the beginning. The html page loads fine. It's just some files in it that have wrong path and won't load.
page loads from http://localhost/level1/
files are trying to load from http://localhost/level2/ and I get 404s.
They should be loading from http://localhost/level1/level2/
So I setup a RewriteRule to correct the path but no matter what I have tried I can't get it to work. I have tried various flags including [R,L] but nothing changes the URI in the html.
currently I have:
RewriteRule ^/level2/(.*)$ /level1/level2/$1 [R]
Any suggestions?
Thanks
Sounds like those links are not relative paths but absolute ones (starting with a leading slash (/). That is why the issue occurs at all. Relative paths make much more sense.
This would be the version to be used inside your http servers host configuration:
RewriteEngine on
RewriteRule ^/level2/(.*)$ /level1/level2/$1 [L,QSA]
Here the version for .htaccess style files (note the missing leading slash):
RewriteEngine on
RewriteRule ^level2/(.*)$ /level1/level2/$1 [L,QSA]
You could use a version that can be used in both situations:
RewriteEngine on
RewriteRule ^/?level2/(.*)$ /level1/level2/$1 [L,QSA]
Note however that in general one should always prefer to place such rules inside the http servers host configurations. .htaccess style files are notoriously error prone, hard to debug and they really slow down the server, often for nothing. .htaccess style files only offer a last option for those who are using a really cheap web hosting provider. Or for situations where a web application has to write its own rewrite rules, which obviously is a security nightmare on its own...

Redirect subfolder requests to subfolder

I have a setup where I can only access the root of a domain (subdomains are not allowed due to restrictive firewall policies). Lets say the domain is www.domain.com, for testing i have a symlinked a folder in /var/www/testing that points to several html and css files. In these files, scripts and css are included like e.g.
<link rel="stylesheet" href="/css/app.css">
which will fail as apache looks for the file in www.domain.com/css/app.css while it resides in www.domain.com/testing/css/app.css is there a possibility with .htaccess or RewriteEngine to make all requests (for css and js files) coming from /testing/ also going to /testing/... like examplified above?
Try
RedirectMatch ^/(css/.+\.css)$ /testing/$1
This will redirect /css/file.css to /testing/css/file.css

Friendly URLs via htaccess

I read lots of tutorials and articles about Friendly URLs through htaccess file, but probably I am missing something.. Thus, I have some questions in order to understand..if someone can help me to clear these out, please!
First of all, before adding any rule to htaccess file,
1. Should I have links like article.php?article_id=1 or the friendly urls I want inside into my files, like: 2016/05/article_name ???
2. I found I link here through another post, that gives a web tool in order to give the rule of the htaccess file: http://www.generateit.net/mod-rewrite/index.php
So, I placed my actual 'ugly' link and gave me right rule to write in htaccess file.
Now, I have uploaded the htaccess file and I am navigating to my website.. If I have the 'ugly' links I can see everything correctly! While I am pressing the friendly url manually there was to issues:
First, I could not see the website's css and javascripts files.. I put the absolute path according to an article, so I guess I am fine with that (is that correct?).
Second, If I press a link let's say the logo, in order to go back to home page, it keeps the rule of htaccess and navigate me to "not found page".. cause the link is something like that: mywebsite.gr/2016/05/index.php, but year and month does not actually exists ...
I would like to have a rule in order to keep the same articles links (I do not have problems about the other links, categories, menus and so on.. just the links of articles). The link I have now is (after all I see and understood):
article.php?article_id=1&year=2016&month=05&name=test-1
and want to have:
mysite.gr/2016/05/test-1.html
*I guess I should mention that the Domain NS does not change yet, and I am working through the temporary link that host provides me.. something like: linux.41.24.23.4.server.gr ... and so on
thanks a lot in advance!! (and sorry for the possible duplicate..)
I think I found a solution... not sure if it is the best one..
DirectoryIndex index.html index.php
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]{4})/([0-9]{2})/([a-z0-9\-]+)\/?.html$ category.php?file=$1&year=$2&month=$3&art_name=$4 [NC,L]
</IfModule>
in order to show my links this way:
mydomain.com.gr/2016/06/this-is-an-article.html
of course inside my website, I slit the article name coming from my DB, in order to add the middle slashes and replace any other symbol with 'nothing':
$art_name = "this is an article!";
$art_name = str_replace('!','',$art_name);
$art_name = str_replace(',','',$art_name);
$art_name = str_replace(' ','-',$art_name);
The final link inside my website is:
click me
Furthermore, about the other links: CSS - Js - a tags and so on, as long as I understood there are some options in order to work properly:
You could use Absolute path, or start your links like this (with forward slash in the beginning):
<link href="**/**main.css" rel="stylesheet" type="text/css" />
it's working like an absolute path I believe, cause the file is seeing the root folder (for example for me is localhost - I am working locally with xampp)
I believe I helped someone who have the same questions..

How can I redirect people accessing my files as directories?

I have the following situation:
On my webserver I have an instance of websvn running, where specific repositories and revisions can be accessed by a URL like
http://www.myhost.com/listing.php?repname=repository1&path=%2Ftrunk%2Fbackend
Somehow, out there in the wild, a wrong URL is being used to access this
http://www.myhost.com/listing.php/?repname=repository1&path=%2Ftrunk%2Fbackend
(Notice the slash after listing.php)
Now, although the URL works and websvn still shows the webpage, images and stylesheets do not get loaded correctly, since they are referenced relative.
I tried to add an .htaccess file to the webroot to redirect people accessing the file as directory to the correct URL.
I have tried multiple variations and ended up with this file:
RewriteEngine on
RewriteRule ^/listing.php/ listing.php [R=301,QSA]
But, since I am writing here, you already guessed it: It doesn't work.
I also tried
RewriteEngine on
RewriteRule ^/listing.php(.*) listing.php$1 [R=301,QSA]
What am I doing wrong?
Perhaps among other things, a RewriteRule within .htaccess that starts with “^/” will never match anything at all. (Examples that include a leading slash are for the global configuration file.) Remove the leading forward slash and see if that helps.
Also, I recommend changing the 301 to a 307 until you get it working. Otherwise, your browser will cache the 301 result, redirecting on subsequent references without consulting your server at all and likely giving you very confusing results.

.htaccess file on xampp (basedir wrong.. incorrect path for images, css files..)

i got another problem.
i have an own apache server (XAMPP) on my computer. The URL in my browser looks like
http://localhost/pageExample/index.php
i use a .htaccess file to change my url from ?action=home to home.html
The problem now is, that the path to all images, css files etc be wrong. They looks like
http://localhost/images/logo.jpg
I think, there is something wrong in my .htaccess file... i tried <base href="http://'.$_SERVER['HTTP_HOST'].'/pageExample/">.. this works ..okay... but some scripts with extern images doesnt work.
My .htaccess file looks like:
RewriteEngine On
RewriteRule ^home.html$ /pageExample/index.php?action=home [L]
(By the way... the problem came since I created the .htaccess)
How to change this?
Thanks!
This is a common problem when you start rewriting URLs but website is not coded accordingly. In your HTML/CSS, when you are referring to images/scripts/css resources you most likely have something like this: <img src="images/hello.jpg" />. Bacuse you are rewriting URLs you need to alter the resource URLs as well -- in the above example resource is linked to the path of the HTML file (relative path) -- you need to make the path absolute. For this -- add leading slash before resource references:
change
<img src="images/hello.jpg" />
to
<img src="/images/hello.jpg" />