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" />
Related
I have a testing site at tektonpress.piconmedia.com that is working correctly.
I'm having some problems with relative URLs and images on a local version.
I installed XAMPP, restored a copy of the database to my local machine, and copied FTP files over. After updating wp-config.php, just about everything is working correctly. That includes images that use relative URLs in my stylesheets.
My problem is with some images that I try to load through HTML. They display correctly on the home page, but not from other pages.
For example, the following HTML works on localhost/tektonpress/ but not localhost/tektonpress/editing/:
<img src="resources/img/logo-white.png" alt="Tekton Press logo">
Here's an example when the image doesn't work:
Example from page
The image in question is located at C:\xampp\htdocs\tektonpress\resources\img\logo-white.png
When I hover over the URL while inspecting the element in Chrome, here's what I see:
One layer too deep
I tried adding a slash to the front of the path, like so:
<img src="/resources/img/logo-white.png" alt="Tekton Press logo">
Unfortunately, the page ends up looking for the image at /localhost/resources/img/logo-white.png.
I've spent several hours on this with no luck. I thought I'd finally found a simple (but potentially maintenance-heavy) solution at stackoverflow.com/questions/7823260/xampp-relative-urls-not-working-correctly, but just ended up with a server error.
Here's the contents of my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /tektonpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /tektonpress/index.php [L]
</IfModule>
Any suggestions?
Try using get_template_directory_uri().'/resources/img/logo-white.png'
It should work. As I am assuming resources is in the base of your theme directory.
I realize it is not much of a fix, but have you tried:
<img src="/tektonpress/resources/img/logo-white.png" alt="Tekton Press logo">
Should let you pull the image properly.
I have problem with creating .htaccess rule for creating url for small resized images.
I have saved images inside upload/images/ directory and it looks like this:
somedomain.com/upload/images/bigimage-some_name123.jpg
I have already created url via PHP so i can call resized image:
somedomain.com/upload/images/small/bigimage-some_name123-150x100.jpg
From this small url I need 4 parameters:
name: bigimage-some_name123
extension: .jpg or jpg
width: 150
height: 100
Via .htaccess need to create url so I can fetch all parameters above and show resized image on the fly:
somedomain.com/image-resizer/index.php?image=upload/images/bigimage-some_name123.jpg&width=150&height=100
Colleague of mine send me this .htaccess line and I need to rewrite it so it can work on my project.
RewriteRule
^upload/images/small/([^/.]+)-([0-9]{3,5})([^/.]+)-([0-9]{2,3})x([0-9]{2,3}).jpg$
module/show_image.php?name=../files/$2$3.jpg&width=$4&height=$5 [L]
This can be well handled in rewrite rules and it is much faster than doing in PHP also. You can use this rule in root .htaccess:
RewriteEngine On
RewriteBase /
RewriteRule ^(upload/images)/small/(.+?)-(\d+)x(\d+)\.(jpe?g)$ image-resizer/index.php?image=$1/$2.$5&width=$3&height=$4 [L,QSA,NC]
You should not try to extract information from the path in the .htaccess rewrite rule. It's much easier doing that in PHP. So what I do is to feed the whole SEO optimized URL to the PHP page behind the rewrite rule. My .htaccess looks like this:
RewriteRule ^(.*)$ /page.php?SEOPath=$1 [NC,L,QSA]
You can see how simple the rewrite rule now is. Then in PHP I work on the SEO path, and extract the wanted information, which is not too difficult.
Of course you still need to adapt this to your situation. For instance, restrict it to .jpg files only. In your case it could be:
RewriteRule ^(.*).jpg$ /module/your.php?SEOPath=$1 [L]
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]
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/" />
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.