htaccess rewrite absolute link folder - php

I have used htaccess to rewrite my links on my page
RewriteEngine On
RewriteRule ^search/([^&]+)$ search.php?q=$1 [L]
RewriteRule ^login/?$ login.php [L]
# etc..
On my PHP pages I have made all my links absolute like this
<?php require("/incl_head.php"); ?>
<link href="/css/css_file.css" rel="stylesheet" type="text/css">
<img src="/images/image.jpg">
This works fine locally, but now I want to test it online, and I have to put it into a subfolder, since my old page is still active.
My subfolder will be test so that my domain will be www.mydomain.dk/test/
But then my pages dosent work, since all my links are absolute, and dont have /test/ in front
Is it somehow possible to make all links have /test/ in front for example using htaccess file (without interrupt my other rules)?
Or what should I do?
Have seen some html base possibilities or some where they save root in vaiable and include in front of all links. But if I add that into a file and tries to include it, I would have the same problem with those includes on every page.
Hope someone can help me

Option 1:
Try this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteRule !^test/ /test%{REQUEST_URI} [L,NC,R]
Option 2: You can try adding this in your page's header:
<base href="/test/" />

Related

Rewrite URL but keep external files

I've been making a web app with PHP but the problem is that when I try to rewrite:
http://example.com/user.php?username=user121
to:
http://example.com/user/user121
with HTACCESS, it loses all external files and only keeps the page's HTML. Please help me out?
My current Rewrite Rule:
RewriteRule users/([^/]+) user.php?username=$1 [NC,L]
Use any one of these solutions.
Solution 1: 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 /.
Solution 2: Try adding this in your page's HTML header:
<base href="/" />
Solution 3: Add this rule in your .htaccess:
RewriteRule users/(.+?\.(?:jpe?g|gif|bmp|png|tiff|css|js))$ /$1 [NC,L,R=301]

Codeigniter loads html code when trying to load style.css or a img file

This is my first time using a php framework and I haven't found an effective solution.
I've made my page load style tag correctly using base_url() in an asset_helper file, and it seems to work fine.
generated code:
<link rel="stylesheet" type="text/css" href="http://localhost/p_maricarmen/assets/css/style.css" media="screen" />
but it doesn't load the css file.
I have the same problem with others files like loading images with img tag.
I also have a .htaccess file in root directory with code:
RewriteEngine on
RewriteCond $1 !^(index.php|public|robots.txt)
RewriteRule ^(.*)$ /p_maricarmen/index.php/$1 [L]
for the friendly url.
For more information, if put the style url in my navigator, it generate an html page with 404 error, page not founded.
Modify your .htaccess file with the below code.
RewriteEngine on
RewriteCond $1 !^(index.php|public|assets|robots.txt)
RewriteRule ^(.*)$ /p_maricarmen/index.php/$1 [L]
The reason why your css file is not loading is that you've to specify the Rewrite Condition for the folder where your css and js files have been placed.
You can do the same thing for your images folder.
Simply specify the images parent folder name as i've specified for the assets, and it should work as expected.
Hope this helps you...

How to link to files and change base url in CodeIgniter?

Hello I've been running into a few issues with CI lately. When I want to link a css file to pretty much anything, I need to put it directly into the root folder. If I move the file somewhere further in the hierarchy it won't load it. Here's an example of when I am trying to link this core_css.css. It loads the one in the htdocs folder, but not the one in the css folder.
Works:
<link href="<?php echo base_url(); ?>core_css.css" type="text/css" rel="stylesheet" />
Does NOT work:
<link href="<?php echo base_url(); ?>application/OBS/css/core_css.css" type="text/css" rel="stylesheet" />
I've tried putting the css filed in all the other subfolders and linking those, but it only loads the one in the root.
My second problem is that every time I try to test a controller, I need to access it through index.php like this:
http://localhost:8888/index.php/test_controller
Is there a way to get rid of the need to put the index.php in the URL?
For your first problem:
Add .htaccess file in your root web directory and write allow from all it means, in this folder your all files and all folder will not give error Access forbidden! use it like this:
<link href="<?php echo base_url(); ?>application/public/css/style.css" rel="stylesheet" type="text/css" />
N.B:
I usually put all my files like that into an "assets" folder in the application root, and then I make sure to use an Asset_Helper to point to those files for me. This is what CodeIgniter suggests.
For your second problem:
If you are using Apache place a .htaccess file in your root web directory containing the following:
RewriteEngine on
RewriteCond $1 !^(index\.php|[Javascript / CSS / Image root Folder name(s)]|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
for better understand you can see codeigniter URL section through this link
http://ellislab.com/codeigniter%20/user-guide/general/urls.html
and another one is http://snipplr.com/view/5966/codeigniter-htaccess/
For the second question: put the following in an .htaccess file inside your CodeIgniter root folder (htdocs in your case):
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?/$1 [L,QSA]
Make sure to set $config['index_page'] to an empty string, and also set $config['base_url'] accordingly (these two options are inside /application/config/config.php)
For the first question: try to make a folder called assets, or something similar and put it in the root of the site (again inside htdocs). Move your css file there, and try to access it by visiting http://localhost:8888/assets/core_css.css.
Also, in case you are using Linux box, make sure the folders you want to access have read permissions.

URL folder hierarchy changes using mod_rewrite in .htacess file

I made an website with php and I wrote .htacess file in my htdocs folder:
RewriteEngine on
RewriteBase /
RewriteRule ^([a-zA-Z0-9]+)/?$ ?mainquery=$1 [QSA]
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ ?mainquery=$1&subquery=$2 [QSA]
My Intention is to redirect the url:
'/A/B' => '/?mainquery=A&subquery=B'.
If the main query is one, everything works fine.
but when the url has two queries, php works fine, but the folder hierarchy moves, so every images and css files with relative urls doesn't work.
It works fine that
(I'm building my web on MAMP, so the domain is now localhost.)
'localhost/publications/articles' => 'localhost/?mainquery=publications&subquery=articles'
but the html thinks the main url is 'localhost/publications', not just 'localhost/'.
so every img tag which has src attribute like
img src='images/myImage.jpg'
doesn't work, because the html thinks the image is in 'localhost/publications/images/', not 'localhost/images/'.
So does css file.
I'm pulling my hair for 3 days, but I can't solve it myself.
How can I solve this problem?
Change this line:
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ ?mainquery=$1&subquery=$2 [QSA]
To this:
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ /?mainquery=$1&subquery=$2 [QSA]
The rewritten rule need to be root-relative, else it's going to be relative to the requested URL, which is in a sub-folder already.
I think that's what you're asking. If you're just asking how to point to images/css/js root-relative, change this:
<img src="images/myImage.jpg" />
To this:
<img src="/images/myImage.jpg" />

RewriteRule to remove extraneous URL parts for pretty URLs

Background
Trying to recreate StackOverflow-style pretty URLs using Apache's mod rewrite and PHP.
Problem
This works great:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ([0-9]+)$ index.php?id=$1 [L]
</IfModule>
URLs like http://localhost/home/script/5 redirect to http://localhost/home/script/index.php?r=5. The CSS content loads flawlessly.
URLs like http://24.68.226.186/recipes/recipe/5/seo-text redirect, but the relative path for the CSS files is then incorrect:
<link rel="StyleSheet" hreF="css/theme.css" type="text/css" media="screen,print" id="css-theme" />
Directories
The directory structure for the script (index.php) resembles:
htdocs/home/script
htdocs/home/script/.htaccess
htdocs/home/script/index.php
htdocs/home/script/css
htdocs/home/script/images
Question
How do you use mod rewrite to tell Apache to use the "script" directory when serving files instead of any sub-directory appended to the URL?
In other words, how would you use mod rewrite to lop off the "seo-text" part of the URL and (1) ensure it reflects the actual title [sourced from a database]; (2) redirect the browser to the new URL [if necessary]; and (3) tell Apache to ignore all paths beyond the ID in the URL?
I would rather not use absolute paths in the CSS.
Thank you!
Used Logan's advice:
<base href="/home/script/" target="_self" />
This does not work with IE8, due to a bug.

Categories