.htaccess referencing images from subdirectories - php

I'll try to make this brief. This is my first attempt using HTACCESS to create "user-friendly URLs". I've set up my .htaccess file so that when a user clicks a link that appears to be in a subfolder, it loads a file within the root.
ex:
RewriteEngine On
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^current-communities/(.*)$ $1 [L]
So if I click a link like "/current-communities/my-community it will load the file "my-community.php". No problem there.
However, now I'm trying to make another rule that when I click a "floor-plans" link, the floor-plans.php file will load data from a database based on its querystring:
RewriteRule ^floor-plans/([^/]+)-([^/]+).php floor-plans.php?communityname=$1&modelname=$2 [NC]
So if a user clicks on a link like "/floor-plans/my-community-myhouse", it will load the "floor-plans.php" file, with "my-community" as the first querystring variable, and "myhouse" as the second.
The issue I'm having here is that the "floor-plans.php" file is not showing the pictures. It is looking into a folder called "images" for graphics, but because "images" is actually within the root directory, I only get a very empty page, but for some reason this is not the issue with the virtual directory of "current-communities".
Can you help?

Hello try using the next code in the head of your document, basically what it does is to set path for all scripts and css files to the actual root of the php document and not what the address in the browser is set up to your using.
so what it actually does is once set in the head before importing anything else php will automatically set the path if you give on css/style.css, php will set it before it goes back to the browser to http://www.myweb.com/css/style.css
it works fine for my wbesites hope it helps you.
<BASE href="http://<?php echo $_SERVER['SERVER_NAME']."/".$_SERVER['PHP_SELF'] ?>">

Related

Rewritten url not being changed in url bar

I made a page which loads content dynamically depending on the alias that was sent. This works fine, but the url stays uglified.
Example:
https://website.nl/aanbieding.php?alias=this-is-the-alias
But I want:
https://website.nl/aanbieding/this-is-the-alias.html
htaccess rule:
RewriteRule ^aanbieding/(.*).html /aanbieding.php?alias=$1 [L]
I got the exact same htacess rule for another page, and that one works like I want.
This one:
RewriteRule ^diensten/(.*).html dienst.php?alias=$1 [L]
When I click from the page itself to another page (same file on the server but different loaded content) the url turns into what I want. Only when I open it from an email template the url stays ugly.
I link from the mail template like this:
Bekijk aanbieding
Why is the url changing into my example?

how to access same directory file in .htaccess

I have facing a problem is when the url is http://localhost/admin/add_department/11 then I clicked a button which under same folder and will go to general_setting.php but the url will became http://localhost/admin/add_department/general_setting and still at add_department page ,
I want it became http://localhost/admin/general_setting,
this is my current .htaccess file content
RewriteEngine On
RewriteBase /
RewriteRule ^(admin)/([\w-]+)/([\w-]+)/?$ test/application/$1/$2.php?action=edit&department_id=$3 [L,QSA,NC]
RewriteRule ^(admin)/([\w-]+) test/application/$1/$2.php [L,QSA,NC]
http://localhost/admin/general_setting is work under my current .htaccess file,how to redirect http://localhost/admin/add_department/11 to http://localhost/admin/general_setting
thanks~
Only the server knows your folder structure. So if you want to go from http://localhost/admin/add_department/11 to http://localhost/admin/general_setting you should make your link point to ../general_setting instead of just 'general_setting'.
Update
A second option is to make all URLs relative to the domain. So if you want to go to http://localhost/admin/add_department/11. You make the link /admin/add_department/11 and if you want to go to http://localhost/admin/general_setting you make the link /admin/general_setting. The slash on the beginning means that it has to look from the domain you're on.
You might have to set a base tag depending on your situation.
<base href="http://localhost">
Documentation of base tag

Using mod-rewrite to pass route to index.php

I'm kind of new to mod_rewrite so I need some help from you guys.
My folder structure is:
assets/fonts/ Contains all fonts
assets/images/ Contains all images
assets/scripts/ Contains all JavaScript files
assets/styles/ Contains all CSS files
pages/ Contains parts of the page as PHP file or directory containing PHP files
header.php Header of site
footer.php Footer of site
index.php index of site, now always loads pages/home.php.
The pages directory contains all the body parts of the pages like home.php, about.php etc. It also contains directories like 'portfolio' which in turn contains more .php files.
This is how I'd like my urls to be re-written.
http://domain.com/about => http://domain.com/index.php?route=about
http://domain.com/about/kittens => http://domain.com/index.php?route=about/kittens
This should however exclude requests directly to the assests or pages folder.
The reason why the pages folder should be excludes on direct requests like http://domain.com/pages/about is because some parts of the site are loaded with AJAX.
Preferable it would exclude any directory in the root.
Could you guys help me out?
You could use an approach along these lines:
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|pages|robots\.txt)
RewriteRule ^(.*)$ /index.php?route=$1 [L]
Save this as .htaccess in your root folder.

mod_rewriter change my url

I have some problems with mod_rewrited at .httacess.
We have created a website, the website links have this sctructure.
www.myweb.com/page.php?title=this-is-the-title&filmID=454122
AND
www.myweb.com/video.php?title=this-is-the-title&filmID=2567971&player=veevr
AND
www.myweb.com/gallery/index.php?galeriID=11
is possible to change this structure to:
www.myweb.com/page/this-is-the-title/454122.html
www.myweb.com/video/this-is-the-title/454122/veevr.html
www.myweb.com/gallery/index/11.html
Any help will be really great
Thank you for reading the post!
Best Regards
JoinOG
In your .htaccess file in your web root folder put the following code.
RewriteEngine On
RewriteRule ^page/([^/.]+)/([0-9]+).html/?$ page.php?title=$1&filmID=$2 [L]
RewriteRule ^video/([^/.]+)/([0-9]+)/([^/.]+).html/?$ video.php?title=$1&filmID=$2&player=$3 [L]
RewriteRule ^gallery/index/([0-9]+).html/?$ gallery/index.php?galeriID=$1 [L]
What this does is mask the url /page/something/12345.html to page.php?title=something&filmID=12345. It masks it, so when you go to the first URL it still looks like the first URL in the address bar but is really at the second URL. Simple tutorial on how this works is here: http://corz.org/serv/tricks/htaccess2.php
The server will think you are in the folder /page/something/ so if your CSS, images and hyperlinks are locally relative links they will not work, e.g. it will look in /page/something/yourimage.png for an image linked to like this <img src='yourimage.png'/>. To get it to work as you'd like it to, you'll need to put a forward slash before all your links to make it relative to your website's root folder like this <img src='/yourimage.png'/>.

after .htaccess url rewrite, cannot perform logoff in some of the url rewrited page

Recently, I was doing .htaccess url rewrite, make all my php url into html, in some page, the logout button wont work properly. for example, in page ‘quotedetails/Q9999.html’ (rewrited from ‘quotedetails.php?quoteID=Q9999′), when I click logout button in this page, it wont do the trick, but when i use the old php url of this page, it works again, other rewrited pages like index.html (index.php), search.html(search.php), all works perfectly.
I use firebug to debug, after I click the logout button, it stays in the same page without redirect me to the index.html, but I saw the the ‘logoff’ params has been passed through, but just dont let me logout and redirect to index page. I’ve changed all the relavent file path to absolute path, still no luck…..help please.
I’ve also noticed from firebug, that page cannot get the redirect ‘location’ as I tried in other pages, their response headers come with ‘location: index.html’, but in that no-workin-page, there is no such line called ‘location: index.html’ in its response headers.
Here is my .htaccess file, no-workin-pages are related to the first four ReweiteRules
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^reps/all,all.html$ rep.php?repID=all&repName=all
RewriteRule ^reps/([A-Z]+),([A-Za-z\sA-Za-z]+).html$ rep.php?repID=$1&repName=$2
RewriteRule ^reps/([A-Za-z]+),([A-Za-z\sA-Za-z]+),([0-9]+).html$ rep.php?repID=$1repName=$2&page=$3
RewriteRule ^quotedetails/(Q[0-9]+).html$ quotedetails.php?quoteID=$1
RewriteRule ^index.html$ index.php
RewriteRule ^addquote.html$ addquote.php
RewriteRule ^search.html$ search.php
RewriteRule ^viewall.html$ viewall.php
RewriteRule ^howto.html$ howto.php
all the CSS will be lost, how to fix this issue?
Use absolute path for all the CSS files and images
I click log out button, its not working
You have to do at least initial debug. Nobody here knows, what's going on when you press a button. Go figure.
You don't have to use absolute paths... most people just forget about one of the most important html-tags. write this into your -section of your html-output:
<base href="http://mysite.com" />
Now all your css-files and images should be loaded correctly.

Categories