Rewrite URL but keep external files - php

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]

Related

my php file doesnt link to css when i clean url using .htaccess

When I use rewrite rules to clean my URL, I can't access my CSS files. Without the rewrite rules my page works fine, but with the rewrite rules images,CSS and JS documents cannot be linked. What could be the problem?
My .htaccess codes
RewriteEngine On
RewriteRule ^index?$ index.php
RewriteRule ^product_details/([0-9]+) product_details.php?id=$1
Now I don't understand why my edit.php is unable to locate external files even though it loads from the correct path on server-side (mysite.com/). And it does not show the URL in an extra directory (in this case mysite.com/e/).
Mod Rewrite will redirect www.example.com/product_details/123 to product_details.php?id=123 on server-side.
Your browser thinks that /product_details/ points to a directory and resolves relative links accordingly.
The solution is to use absolute paths or add <base href="//www.example.com/"> to your html head element.

htaccess rewrite absolute link folder

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/" />

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.

How to rewrite URLs in php5?

Hi I want to rewrite my site URLs. Now I am using PHP 5 for development. I tried with the following .htaccess.
RewriteEngine on
RewriteRule ^(.*)/(.*).html$ $1.php?code=$2 [L]
RewriteRule ^(.*).html$ $1.php [L]
The URL without argument is working, but the first one shows some problems. It redirects to the page, but the page styles are missing whatever value I pass for code.
Why is it happening? Is there any option for rewriting URLs using PHP5?
If you redirect to 'folder/file.html' all of your relative links will be messed up. It will be looking for 'css/style.css' in 'folder/css/style.css' instead.
You can either use absolute links i.e. 'www.example.com/css/style.css' or rewrite your link paths as well.
RewriteRule ^(.+)/css/(.+)$ css/$2 [L]
If you talk about css styling, I think it might be because you're entering a new subdirectory, and the style.css isn't there but in the main directory. It's about relative paths, try using a absolute path ("www.web.com/style.css")
You have your URL in $_SERVER['REQUEST_URI']
which you can parse in your php script and include required file.
In your example 'REQUEST_URI' will be '/folder/file.html'. So you can leave only this RewriteRule ^(.*).html$ $1.php
in html head you set <base href="http://mysite.com/"/> before including css, js & etc.

Categories