Category Archives: absolute-path

Absolute URL from base folder with rewrite

I have a project running local on WampServer. It's an MVC-like structure; it rewrites the URL to index.php?url=$1.

When I want to send the user to another page using PHP location: <location> it doesn't do this properly because of the rewriting (all-though I am technically always in index.php).

For example if I am on http://localhost/project_name/controller/method/ and this controller's constructor or method tries to send me to:

  • header('location: another_controller/method'); sends me to http://localhost/project_name/controller/method/another_controller/method/
  • header('location: /another_controller/method'); sends me to http://localhost/another_controller/method/

But I want it to send me like this:

  • header('location: /another_controller/method'); sends me to http://localhost/project_name/another_controller/method/

Now the only solution I have found is:

define('BASE_URL','http://localhost/project_name');
header('location: '.BASE_URL.'/another_controller/method/');

But this isn't perfect either because it causes me to have to change this defined constant BASE_URL whenever the domain or folder name changes.

Is there a better way. Because this seems like bad practice.

Friendly URL with parameters. stylesheet, script issues

I have a frontend project written in pure HTML + PHP file extension, just for server side include since I'm using an MVC pattern.

For the friendly URL's im using mod_rewrite in the following way

Options +FollowSymLinks
RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

# Remove all .php extensions
RewriteRule ^(.*)$ $1.php

# This is a page with pagination
RewriteRule ^blogs/([0-9]+) blogs.php?page=$1

So URL goes like

http://www.mysyte.com/blogs and

http://www.mysyte.com/blogs/1 // for an additional parameter

Problem is going on the second URL while running the project in Wamp opens the site with no stylesheets at all, or even scripts

I read in one of SO's answers to use base tag

My project is under the folder

wamp < www < project

So to access it on the browser i use

http://localhost/project

Tried with the following <base> tag:

<base href="/project/" />

It didn't worked.

What can I do in this case? I'm kinda stuck

.htaccess url redirect from one an old page to a new one keeping the root directory same

I have an application whose codebase is deployed inside a folder shop . The URL to the main application is http://myapp/shop/.

One of the links on my page reads

http://myapp/shop/website-design/design-urself.php

which I want to redirect to http://myapp/shop/website-design/index.php

The rule which I am using in my .htaccess file is :

RewriteEngine On
RewriteBase /
RewriteRule ^/website-design/design-urself.php /website-design/index.php [R=301]

But the link is wrongly being getting redirected to http://myapp/website-design/index.php

I tried by removing the leading slash (/) from the urls as :

RewriteRule ^website-design/design-urself.php website-design/index.php [R=301]

but still it doesn't work.

I can understand that probably changing the RewriteBase to shop will solve my problem.

However , the thing here is - going further, the folder name ("shop" in my case) can change . So , in that case I have to again go and modify the .htaccess , which is not desirable.

What is the appropriate RewriteRule I should use in this case?

UPDATE : I have my .htaccess file located at my document root , i.e, inside the folder shop and outside of all other folders residing inside shop.

PHP is using Windows-style file paths on Linux [on hold]

I just installed Fedora on my old computer (used to run Windows and worked fine) using a live USB. I have the web root folder at /var/www/html (but my files are in a subdirectory called project/webaccess, so /var/www/html/project/webaccess. I have a folder full of documents at /var/webdocs, with a subdirectory of PHP files to include at /var/webdocs/includes.

I am getting the following error when I run require "/var/webdocs/includes/file.php";:

PHP Warning: require(/var/webdocs/includes/file.php): failed to open stream: Permission denied in /var/www/html/project/webaccess/file.php on line 6, referer: http://myipaddress/login.php

PHP Fatal error: require(): Failed opening required '/var/webdocs/includes/file.php' (include_path='.:/usr/share/pear:/usr/share/php') in /var/html/project/webaccess/admin.php on line 6, referer: http://myipaddress/login.php

I tried creating a file at /var/www/html/project/webaccess/var/webdocs/includes/file.php. It included that file, so it is treating /var/webdocs/includes/file.php like a relative path, Windows-style.

I'm using PHP version 5.4.23. I have run chmod -R 0777 /var/webdocs.

using absolute addressing on a shared host, css and js and images not loading

I have two problem one with absolute addressing and one with mod_rewrite, my project is on a shared host and i have access to :

one.host.com/~user/

i used absolute addressing for images, css and js, and so it is not working, for example my css must be :

one.host.com/~user/style.css

but what is loading is :

one.host.com/style.css

the second problem is that im using mod_rewrite to do this :

one.host.com/~user/register/ -> one.host.com/~user/register.php

does using mode_rewrite effects php includes and requires ? Thanks for your help.

redirect get requests for absolute paths of a particular domain to another domain

i want have added some absolute paths in html, ie get requests to other domains.

<script src="http://www.example.com/dir/file.js"></script>
<script src="http://www.sample.com/js/script.js"></script>
<link rel="stylesheet" type="text/css" href="http://www.example.com/all/home/style.css" />
<link rel="stylesheet" type="text/css" href="mystyle.css" />

i want to redirect all requests to example.com to localhost, 127 0 0 1 this means http://www.example.com/ should get redirected to localhost/

no other requests should be affected

the following code works for redirecting path starting with dir/ , but doesnot work when i use absolute domainname http://www.example.com/ instead of dir to link the file

RewriteRule ^dir/(.*)$   http://localhost/dev/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !/http://localhost/dev/.*

how can i do this using htaccess?

How to get full path from relative path

I'm trying to access a page from another domain, i get all other html from php, but the files like images and audio files have relatives paths making them to be looked inside the local server whereas they are on the other server.

I've allowed cross doamin acess though PHP from the other page.

header('Access-Control-Allow-Origin: *'); 

Then im ajax load to load that page content.

$('#local_div').load('page_to_load_on_side_B #div_on_that_page');

Now, the path looks like

../../user/6/535e55ed00978.jpg

but i want it to be full like.

http//:www.siteB.com/user/6/535e55ed00978.jpg

Correction: I have full access to both site so i need to get absolute path from the site where this files are originating.