How to rewrite URLs in php5? - php

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.

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]

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

.htaccess remove extension while preserving uri parameters

i was trying to rewrite my urls in a friendly way; i have written all the php code to handle everything.
The only thing that i am unable do is removing the .php extension while preserving the uri parameter.
So, essentially, i'm looking for some .htaccess rules to change from this:
www.example.com/biography.php/john-doe
to this:
www.example.com/biography/john-doe
(john-doe isn't a real file, it's only the GET parameter passed to biography.php)
Thanks everyone.
If the links on your site are already like this
www.example.com/biography/john-doe
and you want to process them on the server like this
www.example.com/biography.php/john-doe
add the following to the .htaccess file in the root directory of your site.
RewriteEngine on
RewriteBase /
RewriteRule ^([-a-zA-Z]+)/([-a-zA-Z]+)$ $1.php/$2 [L]
Should do:
RewriteRule ([^\.]).php(.*) $1$2 [QSA,R=301]

Is there an alternative to PHP's $_SERVER['HTTP_HOST']); for apache that does not require a full blown programming language (e.g. SSI)

I have a simple html page that only uses PHP in two places
<?php preg_replace('/(www\.)?([^.]*)\.(com|info)/', '${2}', $_SERVER['HTTP_HOST']); ?>
<?php echo $_SERVER['HTTP_HOST']); ?>
In page is loaded on multiple domains, and I just want to display the host name as text in some other static content
I'd like to remove the need for PHP completely, but not repalce it with another full blown compiler or interpreter. I'd like to avoid using javascript. I can live without being able to do a regex to get the second level of the domain name, but would still like that option. Do I have any options for doing this via a simpler apache module than mod_php?
Theres nothing wrong with mod_php, I'm just seeing if I can minimalize the needs of this website I am working on.
I’d combine both mod_rewrite and SSI. Set an environment variable with mod_rewrite:
RewriteCond %{HTTP_HOST} ^(www\.)?([^.]*)\.(com|info)$
RewriteRule ^ - [L,E=HOST:%2]
And then access that information in SSI with:
<!--#echo var="HOST" -->
I havn't tested it buy you might be able to use htaccess and do a rewrite like this:
RewriteRule (.*) $1?httm_host=%{HTTP_HOST} [L]
I don't know for sure that the %{HTTP_HOST} variable is available in a rewrite but it may work. You may need to use a condition to check for the ? in the URL.
How about JavaScript? You only need two small changes on the site, that can easily be done. So you can plainly serve static HTML files. And to get the domain, you can use:
var domain = window.location.hostname;
Cheers,
Edit: To use mod_rewrite: You will have to set up two identical HTML files, with the correct domain in each. Then you can deliver them via
RewriteCond %{HTTP_HOST} .+\.com
RewriteRule index.html index.com.html [L]
RewriteCond %{HTTP_HOST} .+\.info
RewriteRule index.html index.info.html [L]

Categories