I want to redirect anything after "yy/" to a file xx.php in the same directory.
RewriteRule $ xx.php
If the user enters http://DomainName/yy it will redirect to xx.php with complete css from include('header.php')
If the user enters http://DomainName/yy/ it will redirect to xx.php without complete css from include('header.php')
There must be something that is wrong with my .htaccess entry?
You can place this rule in /xy/.htaccess:
RewriteEngine On
RewriteBase /yy/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^xx\.php$ xx.php [L,NC]
For css/js etc make sure to use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.
Otherwise you can try adding this in your page's HTML header: <base href="/yy/" /> so that every relative URL is resolved from that URL and not the current URL.
Related
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.
I have Rewrite rule as below.
RewriteEngine On
RewriteRule ^inner-subcategory-([0-9]+)$ /inner.php?subcat_id=$1
This rule is working fine. The URL will be like this
http://www/abc.com/inner-subcategory-10
But if I am using '/' instead of '-', CSS can not be applied.
This means, I want a URL like http://www/abc.com/inner/subcategory/10
To use slashes instead of hyphens, replace - by /
RewriteRule ^inner/subcategory/([0-9]+)$ /inner.php?subcat_id=$1
The second part, rewriting the css folder, depends on the requested URL. When the main page is requested as /inner/subcategory/5, then the relative CSS URL will be requested as /inner/subcategory/css/jquery.simpleLens.css. This means, you must add another RewriteRule and rewrite the css folder to an absolute URL
RewriteRule ^inner/subcategory/(css/.+)$ /$1 [L]
Same goes for Javascript files or images.
I'd like to redirect all outside visitors to a holding page whilst allowing all internal users to see the whole site.
I have the following
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} !^1.1.1.1
RewriteCond %{REQUEST_URI} !/holding-page/index.php$
RewriteRule .* /holding-page/index.php [R=302,L]
Which does what I want but the holding page won't pull through any styling or images.
Does anyone know who I'd achieve this?
That is due to the use of relative links on your holding page.
Insert one more rule to fix that:
RewriteRule ^holding/(.+?\.(?:jpe?g|gif|bmp|png|tiff|css|js))$ /$1 [L,NC,R=301]
If that doesn't fix the problem you can adapt these:
use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.
You can add this in your page's HTML header: <base href="/" />
I managed to resolve by allowing access to the /holding-page/ directory with the following rule
RewriteCond %{REQUEST_URI} !/holding-page/
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.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(.*)\.(css|js|htc|pdf|jpg|jpeg|gif|png|ico)$ [NC]
RewriteRule ^(.*)$ index.php?path=$1 [QSA,L]
This .htaccess redirects everything to index.php?path=SOMETHING where I can pick it up (SOMETHING path) and show appropriate content (views).
The problem is with including .css/.js files.
If I use www.website.com/something everything works (css path is like /css/style.css and it's searching in the www.website.com/css/style.css). But if I use www.website.com/something/2 everything crashes because it's searching in www.website.com/something/css/style.css folder.
How to set root (?) everytime to www.website.com (whatever path is)? So I should still have acccess to my 'path' variable but everything should point to the website root and not /some/... path when including my files.
The problem is in the way how you write links to css/js/image files.
Instead of including css/js/images using relative URLs to the current page:
href="css/style.css"
you should use URLs relative to the root (notice the leading slash):
href="/css/style.css"
or use absolute URLs (that will include domain name -- much less preferred approach).
This is a typical mistake when designing website (html part) when site uses Nice URLs/URL Rewritting. This is -- it's now that rewrite engine breaks URLs -- it's browser that sends them like that (/something/css/style.css) in first place.