apache .htaccess url rewrite condition is causing 404 message - .htaccess

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.

Related

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.

Unable to get .htaccess RewriteRule to work

I've been pulling my hair out trying to get a URL rewrite rule to work using .htaccess. Mod_rewrite is enabled and I have managed to get a 301 redirect to work (from /beta to /Beta/) so I know the .htaccess is able to work.
Basically I'm trying to get /Beta/Page.php?id=page&tab=services&tabid=tab1 to become /page/services (and ideally leave out the tabid if it's not going to break the site removing it).
The code I'm working with currently is:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^/Beta/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ /Beta/Page.php?id=$1&tab=$2&tabid=$3
redirect 301 /beta http://www.example.com/Beta/
Any help would be gratefully received.
Remove the leading slash:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(Beta)/[^.]+\.php\?id=([^&]+)&tab=([^\s&]*)&tabid=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/%3/%4? [R=302,L]
RewriteRule ^Beta/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ /Beta/Page.php?id=$1&tab=$2&tabid=$3 [L,QSA]
This will externally redirect:
/Beta/Page.php?id=page&tab=services&tabid=tab1
to
/Beta/page/services/tab1
and rewrite same URI internally.
Also .htaccess is per directory directive and Apache strips the current directory path (thus leading slash) from RewriteRule URI pattern.

htaccess rewrite url with extra slash

I have this code for the htaccess file, and soon I'll have pages where I'd like to create a url like the bottom one
RewriteEngine On
RewriteRule ^Index/?$ index.php [NC]
RewriteRule ^Gallery/?$ gallery.php [NC]
RewriteRule ^Showreel/?$ showreel.php [NC]
RewriteRule ^Music/?$ music.php [NC]
RewriteRule ^Gallery/Render/?$ contact.php [NC]
If I go to the contact page, it'll display a page, but it's entirely white and only contains the text. I'm guessing it's to do with the images not being linked up properly with the new url, but how would I actually go about fixing this without having to manually edit the locations of each image?
You can try adding this in your page's header:
<base href="/" />
or change all of your links to absolute URLs. This issue is most likely that your links are all relative, and when you try to request a URL like:
http://yourdomain.com/Gallery/Render
The relative URI base becomes /Gallery/ instead of / (which is what it is if you access /contact.php directly). And when the browser tries to resolve all the relative links on the page, it uses the wrong URI base.

Circular htaccess rewrite rules

This seems like exactly what you would want to do with the htaccess rewrite rules. But I can't seem to get it to work properly.
I have this page on my site: http://wireie.gocactus.com/network_extensions.php
I want to rename it to: http://wireie.gocactus.com/transparent-ethernet-solutions
So this is the line I put in my htaccess file:
RewriteRule ^transparent-ethernet-solutions network_extensions\.php [NC]
Alrighty. Now if I go to /transparent-ethernet-solutions, it works great.
The problem is that, /network_extensions.php also still works great. So I'll want to rewrite that url with this line:
RewriteRule ^network_extensions\.php*$ /transparent-ethernet-solutions [R,NC]
Now it gives me an error message about too many redirects. If I add the second line without the first line, it changes the URL on the PHP link but gives me a 'page not found' error on the pretty link redirection.
My mind is blown. Can I have the URL rewritten without forwarding and have the new URL work without redirecting from the old one? I'm not sure if there's just some parameter I'm missing in the first line, or some combination of rules necessary.
This is the code that will be needed in your .htaccess:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^transparent-ethernet-solutions/?$ network_extensions.php [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+network_extensions\.php\s [NC]
RewriteRule ^ transparent-ethernet-solutions [R=301,L]

htaccess rewrite all to index.html

I'm trying to write the .htaccess file so that whatever the user requests, he will have the page index.html
I've written this:
Options +FollowSymlinks
RewriteEngine on
RewriteRule .* index.html [NC]
I understand that this will cause: whatever the incoming request URL is, i.e, www.domain.com/*** (whatever comes after the slash), the result will be the page www.domain.com/index.html
However, I'm getting a Server error. What am I missing?
NOTE: I don't want it to be permanent redirect, I'm just trying to "hide" the content of my site for a couple of hours with that index.html page (which says that the site is under maintenance).
If you want to redirect everything to temporary maintenance page, you can do :
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/maintenance.html$
RewriteRule .* /maintenance.html [L,R=302]
the R=302 flag is used to generate a temporary redirect
Try to remove Options +FollowSymlinks , some servers won't allow you to overwrite a php.ini setting.

Resources