htaccess redirect image folder to root - php

I have a directory called "public_html/hammer/" (my CMS). I want to use the image folder that is on the root "public_html/". I could simply do something like "../picture.jpg" but the content within "/hammer/" will be show on the root directory so it will then be broken when I query the html.
How can I write an ModRewrite or similar that will point "public_html/hammer/images/" to "public_html/images"?

Assuming public_html is your document root, place a rule like the following in public_html/.htaccess:
RewriteEngine On
RewriteRule ^hammer/images/(.*)$ images/$1 [L]

Basically:
RewriteEngine on
RewriteRule ^.*$ ../../$1 [L]

Related

Remove public_html from url through htaccess

I have a legacy project on a server where the actual framework is in a folder above the public_html folder. So the directory structure is like this:
domainname.com
app/
framework/
public_html/
index.php
Now I want to place this on our own server. This is an application created by the hosting company. The root folder is default. So now my directory structure is like this:
default/
app/
framework/
public_html/
index.php
Now he's pointing to the default folder instead of the public_html folder. That's the only difference with the old version. So I've added a .htaccess file to the root folder default. The content of this file is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /public_html/$1 [QSA,L]
</IfModule>
In my public_html folder I have a .htaccess file like this:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule . index.php [QSA,NS]
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule . /index.php [L,QSA,NS]
So when I go to my website it works. But when I click on a link I get: http://domainname.com/public_html/theclickedpage/.
But I don't want the public_html in my url. How can I fix this?
You may want to search for a way to make a virtual host. I know some of my webhost let me configure the vhost through my admin page. It will let your server consider the url without including the specified folder, but back in the stage it performs needed redirection.
You applied correct rule ,
but the problem is link for the solution of this issue you have to
remove public_html from
your all links and it will automatically redirect to your required
path without public_html.

PHP / .htaccess - Redirect to specific folder

I am creating a PHP project, and the folder structure looks something like:
includes/
pages/
config/
In theory, all of the pages will go into the pages folder. But, whenever someone visits the website, on a specific page: (i.e.) www.mysite.com/help I want it to look inside the pages/ folder, rather than thinking it is on the root of the document.
Can I achieve this using PHP / .htaccess - I have googled this problem and cannot see any relevant infomation
You can use the following rule in /root/.htaccess :
RewriteEngine on
#if "/document_root/pages/foo" is an existent file
RewriteCond %{DOCUMENT_ROOT}/pages/$1 -f
#rewrite /foo to /pages/foo
RewriteRule ^(.*)$ /pages/$1 [NC,L]
Apache supports URL rewriting as explained here:
http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
.htaccess
RewriteEngine on
RewriteRule ^/?help/?(.*) /pages/$1 [L]

Making server redirect to another folder with .htaccess

Can i make a htaccess file that redirect from the root of my project to another folder.
So if i usually write 192.168.1.39/public to get to my library where i store the files that i want the users to see.
I would like to only write 192.168.1.39 instead.
Can you do that with a .htaccess file?
In the htaccess file in root, add this rule :
RewriteEngine on
RewriteRule ^$ /folder [NC,L]
^$ matches the hompage and rewrites it to the folder.

How do I get mod-rewrite to access the proper directory?

I'm trying to use mod-rewrite to eliminate urls like {root}/index.php?action=about&page=faq. I want {root}/about/faq to redirect to the php url. My .htaccess file looks like
DirectoryIndex index.php
RewriteEngine On
RewriteRule ^(index|about|calendar|news|contact)$ index.php?action=$1 [L]
RewriteRule ^(index|about)/(.+)$ index.php?action=$1&page=$2 [L]
When redirecting to index.php?action=$1, this works fine. However, it doesn't work completely when the second rule is used. The html content appears fine, but the stylesheet and images are missing. The page seems to think that it's in a subdirectory. So {root}/about/faq looks for images in the folder about/images, instead of in the images folder found at the root. The PHP script says that the working directory is still the root directory, however. How do I make the pages think that they are in the root directory? Thanks!
RewriteRule ^(index|about|calendar|news|contact)/(.*)/$ /index.php?action=$1&page=$2
Would result in /about/faq/ rewriting to index.php?action=about&page=faq
/calendar/month/ would result in index.php?action=calendar&page=month
Also unless you want to rewrite imagesfiles I would add:
RewriteCond %{REQUEST_URI} !(\.gif|\.jpg|\.png)$ [NC]
This will stop image requests being rewritten.

files inside root directory not accessible mod_rewrite PHP

The file structure looks like this
root
root/x
root/x/y.php
root/index.php
.htaccess code
RewriteRule (.*) /x/$1 [L]
What the htaccess file does is to remove the folder name (x) from the url so accessing y.php = http://localhost/y.php instead of http://localhost/x/y.php . This works but my problem now is the index.php shows something like this:
Index of /x
Parent Directory
y.php
I can't access the index.php. I believe the x became the root folder.
Thanks for your help!
First, you shouldn't allow index displays like this on a public server ... on your localhost it may be okay, but still, you can disable it in your .htaccess by adding this:
Options -Indexes
To address your question, you should probably add the following conditions before your rewrite rule so that the rule won't apply to any files or directories that actually exist on disk:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
This won't fix the issue you have because the .htaccess is redirecting to a non-existant /x/index.php file ... this is why it's showing the index listing for the /x directory

Categories