RewriteRule giving 500 error when adding dynamic id at url end - .htaccess

I am getting a 500 Internal Server Error from my linked CSS files etc when I use this htaccess code.. Anyone know what might be the problem? I am not too fluent in htaccess yet.
Here is the code:
RewriteEngine On
RewriteBase /
RewriteRule ^(system|img|res) - [L]
RewriteRule ^picture/([^/]*)/?$ picture.php?id=$1 [L,QSA]
## The below code is something I found on the internet to remove the .php tag
# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{HTTP_HOST} ^www\.mywebsite\.com
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)index$ $1 [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
The URL is supposed to be: www.mysite.com/pictures/1 (id)
The id is always a number.
It does show me the page and I can echo the id, so that part is working, however it gives me a 500 error on linked files as mentioned above.

Not sure why it acts like that tho.. The CSS folder is in the same folder as the actual php file.
You've linked to it using a relative URI:
<link rel="stylesheet" type="text/css" media="all" href="./css/text.css" />
e.g. the ./css/text.css, and while the css file may be in the same directory as picture.php file (which I assume is what is generating the content) but the browser is what actually makes the request for the CSS, not the picture.php script. The browser requests this URL http://www.mysite.com/picture/1, and the server internally rewrite the /picture/1 to /picture.php?id=1, the browser has no clue that's happened. So it sees the base URI as /picture/. If the browser went directly to the php file: http://www.mysite.com/picture.php?id=1, the base URI would be / and the css would resolve just fine to /./css/text.css. But the /picture/1 request has a different base URI so the browser (with no clue that the base is different) blindly attempts to retrieve the css as /picture/./css/text.css, which fails because you have rules that mishandle that URI. Normally you'd just get a 404, but the rules you have after the picture rewrite mishandles the URI and returns a 500 server error.
You can either add in your header:
<base href="/">
in the content generated by picture.php, or make the URI's absolute:
<link rel="stylesheet" type="text/css" media="all" href="/css/text.css" />

Related

htaccess - Load css, js, images

I write htaccess with code:
RewriteEngine On
RewriteBase /MVC_PHP_Basic
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ index.php?link=$1 [L]
RewriteRule ^([^/]*)/([^/]*)$ index.php?link=$1&action=$2 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ index.php?link=$1&id=$2&action=$3 [L]
And it works and My css,js, image is loaded with url localhost/users but is not load with url localhost/users/detail.
Errors: can't load file css,js.
I know this error is due to path of css,js file because i use:
<link rel="stylesheet" type="text/css" href="assets/css/bootstrap-grid.min.css">
When url is "localhost/users", href is "localhost/assets/css/bootstrap-grid.min.css" (right).
And when url is "localhost/users/detail", href is "localhost/users/assets/css/bootstrap-grid.min.css" (wrong).
Please tell me how to fix (I don't want to use "absolute path").
Try to put / like this "/assets/css/bootstrap-grid.min.css" and place your asset folder on the root folder, or you will have to use like this "/users/assets/css/bootstrap-grid.min.css"

htaccess | not looking in the right location for js

I am trying to write a htaccess file that will allow for /example/1/profile to look for a JavaScript file within /example/. Currently on Internet Explorer 11 it is looking for /example/1/file.js whereas realistically it should be looking for /example/file.js.
This needs to be done inside of the .htaccess file as the setup that the website currently has.
I know there is a way in which you can redirect 404 to /example however this is resulting in a 200.
Is their a way I can say in the htaccess file that if it is .js .css to look in /example?
Thanks in advance
EDIT:
For a little but more information, my current htaccess is like this
DirectoryIndex index.php index.html
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /example/index.php [NC,L]
It is for php because the php echos a file_get_contents of the index.html which is an Angular project.
So I need this htaccess to be the following logic
If the file is a .js or .css then rewrite the location to /example else rewrite the location to example/index.php.
The reason this is happening is because I am doing a format which has the ID as a second parameter and for some reason this is interfering with the way that the URL is structured for the js, css.
I imagine this line is what is breaking it...
RewriteRule ^(.*) /example/index.php [NC,L]
Converting my comments to answer. This appears to be problem due to relative links.
To fix, you can add this just below <head> section of your page's HTML:
<base href="/example/" />
so that every relative URL is resolved from that base URL and not from the current page's URL.

Format url to friendly url using .htaccess

I try to format my url to friendly url by using htaccess
This is my url
http://localhost/index.php?go=product&id=32
I'm using REQUEST METHOD
$id = $_REQUEST['id'];
//Do some thing with php code to get info product from Mysql
$Title = $row['title'];
//Example: Computer 4GB 2CPU
But when I want to SEO my website, it's not good, I wish my url like this:
http://localhost/product/32/Computer-4GB-2CPU
So in your .htaccess, add this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^([0-9a-zA-Z]+)/([0-9a-zA-Z]+)/([0-9a-zA-Z]+)$ index.php?go=$1&id=$2&product=$3 [NC,L]
In your <head> add the following:
<base href="/">
So that the images and CSS work.
Add a get method for product to get the name, it should work, if it doesn't go ahead and comment.

apache .htaccess url rewrite condition is causing 404 message

My .htaccess looks like this:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^VIN/(.*)/(.*)/ index.php?action=vin&view=$1&vin=$2
I can go to my url (foo.com) and it works just fine. However, if I go to foo.com/VIN/vinDetails/12345, I get a 404 error.
I'm new to the URL re-write, and have looked everywhere to try and figure this out, but I keep coming up short.
You can make trailing slash optional:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(?:css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^VIN/([^/]+)/([^/]+)/?$ index.php?action=vin&view=$1&vin=$2 [L,QSA,NC]
css/js loading is due to your use of relative paths. Make sure path of these files start either with http:// or a slash /.
Otherwise 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.

Rewrite Rule causes "partially encrypted connection" in Firefox & Chrome

The question is based on the question htaccess rewriterule: redirect http to https or https to http (both ways) depending on URL typed. The great solution (thanks to Ulrich Palha) looks like this:
RewriteEngine on
RewriteBase /
#determine if page is supposed to be http
#if it has p=home or p=home1 or qqq=home in querystring
RewriteCond %{QUERY_STRING} (^|&)(p=home1?|qqq=home)(&|$) [NC,OR]
#or if query string is empty
RewriteCond %{QUERY_STRING} ^$
#set env var to 1
RewriteRule ^ - [E=IS_HTTP:1]
#all pages that are supposed to be http redirected if https
RewriteCond %{HTTPS} on
RewriteCond %{ENV:IS_HTTP} 1
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [R,L=301]
#all other pages are sent to https if not already so
RewriteCond %{HTTPS} off
RewriteCond %{ENV:IS_HTTP} !1
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L=301]
Unfortunately, once I add this code to .htaccess file, everything works fine, but https started look broken, if contains links to css or pictures from the server, although the paths are relative.
The page below (https://www.example.com/?p=welcome) displayed with a broken SSL connection, FF browser says "Your connection to this site is only partrially encrypted"
<head>
<title>ddd</title>
</head>
<body>
Hello
<img src="/gfx/stars/help.gif" alt="" />
</body>
</html>
Why?
Once I remove <img src="/gfx/stars/help.gif" alt="" /> from the page , FF displays https fine, the URL bar is green then (SSL certificate is displayed). The same error appears, when I test it using Google Chrome.
So, what's wrong with <img src="/gfx/stars/help.gif" alt="" />? The path is relative.
The same happens if I add
<link rel="stylesheet" type="text/css" href="/css/mycss.css" />
Once the page has loaded and the browser starts requesting images and so on, over https. By my reading of your rewrite rules, because the path to the image has no query string IS_HTTP is set to 1. This then causes your rewrite rules to do a redirect to the http version of the image URL which is why the browser complains
One way to fix this is to avoid processing resources (since you don't have any special rules for them anyway) as follows
RewriteEngine on
RewriteBase /
#if its a resource (add others that are missing)
RewriteCond %{REQUEST_URI} \.(gif|css|png|js|jpe?g)$ [NC]
#do nothing
RewriteRule ^ - [L]
#rest of existing rules go here

Resources