mod_rewrite changes MIME types .css/.js -> html - apache

I have an issue with mod_rewrite, I have a .htaccess file set up in directory called cms:
Options +FollowSymLinks
RewriteEngine on
#rewrite rules for edit
RewriteRule ^edit\/(.*) edit.php?page=$1 [QSA,L]
I would like to access it like so sitename.com/cms/edit/2 but when I do I get errors:
When I access the natural path (sitename/css/edit.php?page=2) everything works fine.
Any help would be appreciated.

While this is an old issue, i just experienced the same thing but then when i used RewriteRules.
The solution for that turns out to be:
RewriteRule ^bower_components/(.*)\.(css) test/bower_components/$1.css [L,T=text/css]
RewriteRule ^bower_components/(.*)\.(js) test/bower_components/$1.js [L,T=application/javascript]
To force the type of the files you are rewriting. Targeting css + js specifically and forcing the T=<>

Not sure if you had a typo, what about changing:
RewriteRule ^edit\/(.*) edit.php?page=$1 [QSA,L]
To:
RewriteRule ^edit/(.*?)/?$ edit.php?page=$1 [QSA,L]

I know its old question, but just in case someone will still search for an answer.
I just witness same errors.
The deal is: with L parameter mod_rewrite will point request to the location you specified but all the ajax calls, includes, javascript src requests will behave as you where under original location.
Check the headers using LiveHeaders in Firefox for instance and you will see that your requests are pointing to wrong locations. I.e.: if in your play.php you link javascript like
src="myJS.js"
if RewriteRule points from
[siteroot]/play/some_data_here
to
[siteroot]/play.php?data=some_data_here
you will see that your play.php file will try to include src="play/myJS.js" which of course is not there so it returns 404 aka text/html.
In my case I 'fixed it' by adding R parameter so it redirects to new location. Down side of this is that address in the browser will change. Try changing [QSA,L] to [QSA,L,R].
Hope this helps.

I was having this problem as well and found a solution.
If your css / js files are linked with a relative path, your RewriteRule will also affect them. To avoid this, reference the root directory. In other words:
<link type="text/css" rel="stylesheet" href="css/style.css">
becomes
<link type="text/css" rel="stylesheet" href="/css/style.css">

Related

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..

Allowing relative paths only

I believe this is an Apache .htaccess issue, and yet, maybe not. Maybe I am looking at the problem from the wrong angle, and thus can't find the proper solution.
I am building a web app + hybrid mobile app. I would like to share the exact same code base, without having to tweak anything manually to deploy my app to Android or iOS, otherwise, the process of deploying will be hacky and painful. What I want is to take the web app repository, shove it into Cordova's box (you dirty man ;), and it would deploy it successfully.
Now, one issue is that Cordova requires relative paths to work properly. For example, this is how I include my require.js file :
<script data-main="library/js/dependencies.js" src="library/js/libs/require.js">
</script>
This works fine on the hybrid app. This works fine also on most of the web app's URLs, those with the following scheme :
domain.com/view_name
However, this is what happens when I load the app from a view that receives URI parameters :
domain.com/view_name/6iwO4NyJqy
The relative paths are not resolved properly anymore. I get 404 error due to unproper paths. For instance, this is how is resolved the require.js file above :
http://domain.com/view_name/library/js/libs/require.js
The view_name bit is the wrong part. It should not be there. Without it, the file would be found successfully.
This is my .htaccess file :
RewriteEngine on
RewriteBase /
# REROUTING EVERYTHING TO index.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteRule .* /index.html [NC,L,QSA]
Is there a way to set my .htaccess file, so that I don't need to modify the relative paths within the app, and still can have them resolved properly ?
Any suggestion is most welcome.
It is not caused by your rewrite rule, it is due to your use of relative paths.
You can add this just below <head> section of your page's HTML:
<base href="/" />
so that every relative URL is resolved from that base URL and not from the current page's URL.

mod_rewrite rule error when using /

I have the following RewriteRule in my .htaccess file:
RewriteRule ^advanced-lift-truck?$ pub-listing-full.php?mag=1 [NC,L]
How could I modify this so that .../advanced-lift-truck/ works, as well as .../advanced-lift-truck? (Note the / in the first version.)
I'm assuming I'll need a separate rule, but I've tried the following also:
RewriteRule ^advanced-lift-truck/?$ pub-listing-full.php?mag=1 [NC,L]
but this forwards me to pub-listing-full.php without the query string, and without any CSS/JS files loaded.
Not sure why the query string is missing, but you need to deal with all of your relative URLs in the links of your page. With the extra /, browsers assume the base URI to resolve any relative links (to stuff like style sheets and scripts) is /advanced-lift-truck/. Try adding this to the header of your pages:
<base href="/" />

how to rewrite url with htaccess and keep relative links working

I am using the following .htaccess rules:
RewriteRule ^!([a-zA-Z0-9_-]+)$ index.php?p=$1
RewriteRule ^!([a-zA-Z0-9_-]+)/$ index.php?p=$1
RewriteRule ^!([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ index.php?p=$1&s=$2
RewriteRule ^!([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ index.php?p=$1&s=$2
In order to rewrite the url from this: /index.php?p=SOMETHING&s=SOMETHING
To this: /SOMETHING/SOMETHING
The problem is - I am using relative urls for pretty much everything - css styles, scripts, images, etc.
And the current htaccess rules break the urls, cause they are trying to access files from a realtive path.
So I would like to know if there is any way to be able to still use these rules and keep the relative links working at the same time.
Any help is much appreciated!
You need to include the URI base in the header of your pages. You can add something like this in your page headers (inbetween the <head> </head> tags:
<base href="/">
Either that, or you can change all your links to absolute.

RewriteRule causes page to reload twice

I shaped two different RewriteRules for my page:
# Enable URL Rewriting
RewriteEngine on
# exclude followed stuff
RewriteRule ^(js|img|css|favicon\.ico|image\.php|anprobe|content|libs|flash\.php|securimage)/ - [L,QSA,S=2]
# conditions (REQUEST dont point # file|dir|link)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-F
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
# rules
RewriteRule ^(?!index\.php)brillen/(.*(brillen)|360|neu)/(.*)([a-zA-Z0-9]{5}-[a-zA-Z0-9]{5}(?!\.))(.*)$ /index.php/brillen/$1?art_id=$4&$5&%{QUERY_STRING} [NS,QSA,L]
RewriteRule ^(?!index\.php)(.*)$ /index.php/$1 [NS,QSA,L]
... and I'm encountering a strange problem, which lies in every request causing the page internally to load twice, which leads to the problem that db actions and email dispatching are also executed twice.
Does anyone have an idea concerning that?
Thanks in advance!
Note 1: All requested resources are valid and available according to the browser's resource tracking.
Note 2: May the problem originate in retaining and post-processing the PATH_INFO? (/index.php/$1 => /index.php/foo/bar/...)
The rewrite Engine cannot make a single HTTP request run twice. It routes the HTTP request for Apache to either a static file, a proxy function, or a module (like PHP) with alteration in the request. But it cannot clone the request and give it 2 times to apache.
When you have any "run twice" problem chances are that you are hit by the empty image url bug. In fact it's not really a bug it's a feature of HTML (at least before HTML5) and a feature of url-parsing.
If you get somewhere an empty GET url, HTML states that the browser should re-send the same query (the one that gave him the current page) with same parameters. This can make a POST request happen 2 times (if the requested 1st page were a POST). So where are these empty GET url? Most of the time you get either :
<IMG SRC="" ...> (in the HTML)
or:
url() (in the css)
or:
<script type="text/javascript" src=""></script>
<link rel="stylesheet" type="text/css" href=""> (in the HTML headers)
Read also #Jon answer about the favicon query. You should always test the result without browsers behaviours by using wget or telnet 80 queries.
Update: detailled explanations and followups available on this blog with HTML5 additions which should remove this behavior for modern browsers.
I had the same issue (or so I thought). It was caused by the request for favicon.ico, which I hadn't considered in my rewrite rule.
I had the same problem, caused because I did some url rewriting, and the script was being loaded twice, due to the fact that i did not add this:
RewriteRule ^(js|img|css|favicon\.ico)/ - [L,QSA,S=2]
This will stop the script from being loaded twice; it solved my problem.