htaccess rewrite by making friendly url will not work - .htaccess

I am trying to make my url from this:
www.domain.com/view.php?id=15
To ths:
www.domain.com/watch/15
My code in htaccess is as follows:
Options -Indexes
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^watch/([^/]+) view.php?id=$1 [NC]
Now if I go to www.domain.com/watch/15 it loads the content but without any js, css because in this case it looks for css and js under /watch folder which doesnt exist..
How do I make this option to work properly? I dont want to create watch folder and copy paste css and js so it will embed properly

You need to add the base tag to the head section of html to fix the css/js problem.
<base href="http://www.yousite.com" />
Then your rules will work fine. I modified them a little as well for good measure.
Options -Indexes +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^watch/([^/]+)/? view.php?id=$1 [NC,L]

Related

Why below .htaccess code not works correctly

Below code is to make my url look pretty
RewriteEngine on
RewriteRule "^info/(.*)$" "/detail.php?id=$1" [L,NC]
detail.php will retrieve data from database based on ID we send to it, but page will be display as it has no css and JS file
it displays as below:
While it should be like this
Try with this :
Options +FollowSymLinks -MultiViews -Indexes
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^info/(.*)$ detail.php?id=$1 [L]
And add before your CSS & JS links this: http://example.com/your_files.css
The problem is from your links! Just add domain before them, and everything will work :)
Or just add this to your <head> tag : (Specify a default URL and a default target for all links on a page)
<base href="http://example.com/">
See this, it's the solution to your problem :)

URL Rewrite Rule shows blank page with text

i'm learning how to rewrite urls and im trying to do that in my ubuntu server. I managed myselft to activate the Allowoverride All and i have htacess working.
I want to change this url for example:
http://serverismai/myapi/public/rest/navigate.php
And i try to do it like this :
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^home(.*)$ myapi/public/rest/curso/navigate.php/$1 [L]
</IfModule>
I've tried various solutions but this was the closest i got. The url dosent change automatically but if i insert http://serverismai/myapi/public/rest/home i see a blank page with some of my website html written ( not even the html of the page i'm trying to rewite url).
I'm pretty new to this so i want to know if there is something wrong in the Rewrite Rule or if my error is somewhere else...
Thank you!
You can use this code in your /myapi/public/rest/.htaccess file:
RewriteEngine On
RewriteBase /myapi/public/rest/
RewriteRule ^home/?$ curso/navigate.php [L,NC]

.htaccess flat link

I want to to use flat links like this:
http://somedomain.com/about-us
http://somedomain.com/products-list
http://somedomain.com/product/item
Thus I've used the mod_rewrite rules like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9A-Za-z-]+)?$ /index.php?page=$1 [L]
RewriteRule ^([0-9A-Za-z-]+)/([0-9A-Za-z-]+)?$ /index.php?page=$1&value=$2 [L]
</IfModule>
The first two links are working fine. But whenever I visit the third type of links the images or the css or any js script that are linked as relative path eg. <img src="images/image.jpg"> and About Us.
The browser thinks it is in http://somedomain.com/product/images/image.jpg and http://somedomain.com/product/about-us.
The correct path should be http://somedomain.com/images/image.jpg and http://somedomain.com/about-us
And thus files with relative links are not working.
How to fix this issue? I don't want to use full path instead of relative for all linked files. Please suggest a way by doing some tweaks in the .htaccess
Try escaping your slash:
RewriteRule ^([0-9A-Za-z-]+)\/([0-9A-Za-z-]+)?$ /index.php?page=$1&value=$2 [L]

.htaccess rewrite url breaks css/javascript/other files

my .htaccess file looks like:
RewriteEngine On
RewriteRule ^([^/]*)$ index.php/?node=$1 [L]
now when I include my css file with:
<link href="sitewide.css" rel="stylesheet" type="text/css" />
it doesn't work. The same goes for javascript.
I've been looking everywhere for an easy solution that wouldn't make me hardcode baselinks for all my relative paths, like imgs, css, javascript...
So here it goes, add this between the <head> tags of the pages you are having problems:
<base href='http://www.mydomain.com/'>
This will make that your relative path links will start with this base link. Simple as that.
The <base> tag specifies the base URL/target for all relative URLs in a document. Put the <base> tag as the first element inside the <head> element, so that other elements in the head section uses the information from the <base> element.
Did it work for you?
Consider rewriting only non-existing paths, e.g. if file or directory exists - don't rewrite it.
Wordpress uses this to rewrite their permalinks, I think it's pretty good example:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
As you can see they have rewrite conditions to exclude existing files/directories.
That .htaccess file is redirecting anything that doesn't include a / to index.php.
Consider rewriting it to catch only the paths you really need.
Alternatively, you could start your file with a rule indicated as "Last" (L):
RewriteRule ^/javascript.js$ $0 [L]
This would force Apache to evaluate this RewriteRule and then halt all other rules for that request. Thus, your .js or .css files would be served properly, but you'd have to make a new entry in the .htaccess for every such file. I'd strongly recommend my first suggestion over this one.
Try this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^([^/]*)$ index.php/?node=$1 [L]

error in htaccess not read my images and css file

i create .htaccess and when run the page all images in my site not display and css not run.
images in the folder (images/) and css in(css/).
another problem the url now is http://localhost/mysite/progams/4
and when click link inside this page will be http://localhost/mysite/progams/program/6
please any one have a solution?
.htaccess code
Options +FollowSymLinks
RewriteEngine On
# Turn on the rewriting engine
RewriteRule ^program/([0-9]+)/?$ get_programs.php?pack_id=$1 [NC,L]
RewriteRule ^El-Mensajero/?$ home.php [NC,L]
RewriteRule ^Daily-Tours/?$ daily_tours.php [NC,L]
RewriteRule ^page/([0-9]+)/?$ get_pages.php?page_id=$1 [NC,L]
thank you
You should not rewrite static files. Where are your images/css files located?
A rewrite rule like this will prevent files existing on disk not to be rewritten:
RewriteCond %{REQUEST_FILENAME} !-f # Do not rewrite static files
RewriteCond %{REQUEST_FILENAME} !-d # Do not rewrite static directories
As for your second question, please clarify, im not sure i understand what you mean.

Resources