Problems with user-friendly URLs on localhost - php

I am trying to use a htaccess file on my localhost in order to create user-friendly URLs. But I've not gotten a happy ending so far: I can't make it work properly. Here are the details:
I have the Apache running on Windows. My localhost is here: C:\AppServ\www\, so I accede to my local site ("university") through the next path with the browser: localhost/university
I have this url:
localhost/university/university.php?filter=private
And I want to be read simply like this:
localhost/university/university/private
So, I write the next code into my htaccess file (the file is into this folder C:\AppServ\www\university)
RewriteEngine On
RewriteRule ^university/private$ university.php?filter=private
That doesn't work. That opens university.php without any style. (In fact, when I have no htaccess file, if I try to accede to localhost/university/university/private, I have no 404 error; I have the same university.php without any style instead.)
But I have success when I write the next rule:
RewriteRule ^private$ university/university.php?filter=private
This work fine to localhost/university/private but not to localhost/university/university/private
I have no clue about where is the problem. Do you have one?
Thank you so much!!

That opens university.php without any style.
This is probably because you have relative URI's to access your style sheets, and the extra path node (/private) is making the browser think the styles are in the private folder, which doesn't exist. Try adding a relative URI base to the header of your page:
<base href="/university/" />

Related

can someone clarify issues with root relative URL and htaccess

I am rewriting some of my URLs using the htaccess engine but I am running into this issue that I really don't understand.
Using the following rule in htaccess
RewriteRule ^category/(\d+)$ category.php?category=$1 [NC,L]
I set the relative path in HTML to <a href='category/{$cat_id}'>{$catTitle}</a> this works fine if I am going to this link from another page. Although if I open the link from the same page the resulting URL becomes http://localhost/CMS/category/category/1 which is not valid. Someone proposed that I should use what is called "root-relative URL" which requires changing the path to <a href='/CMS/category/{$cat_id}'> that solved the problem. What I am trying to understand why <a href='category.php?category={$cat_id}'>worked without using the root-relative URL and adding the /cms/ before using htaccsses does that mean I have to change each URL I have in my code and add root-relative URL? Is this how htaccess engine works with paths?
Thank you.

How to update absolute URLs for media files for a whole site at once?

I have moved a whole site to a new domain. However, some of the <img src> urls are absolute and are no longer pointing to the files in the correct
(new domain) location. The relative URLs are working, since the file structure is the same as the original domain. How can I redirect the URLs for the media files in one place for all the page files?
I have tried to set redirects in the .htaccess file but no luck.
Here is the most current version of the redirect I tried to use, but it seems to only works for page urls, not the media absolute urls in the body of the pages:
#Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)$ http://www.newsite.com/$1 [R=301,L]
I am looking for detailed instruction on where to put code etc. to redirect all of the absolute URLs in my site. I am a rookie, so please be clear and kind. Thank you for your help.
separate the files in sizable chunks based on your pc's capabilities into folders and open them all in a text editor - search and replace, then go from there. Let me know if this helps or if I am completely off my rocker. : )

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 url rewrite rule not working as expected

I have a complex problem that I an unable to solve for days now. Maybe some expert with more knowledge of htaccess functionality will be able to help out.
I have two files placed in the root directory - test.php and files_include.php.
The URL that a user would normally see is:
www.example.com/test.php?cs1=A&cs2=B&cs3=C&cs4=D
Since this is a ugly URL I would like to rewrite it to something better like:
www.example.com/search/A-B-C-D.html
Using a rule in .htaccess like this I can easily rewrite the URL:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^search/([^-]*)-([^-]*)-([^-]*)-([^-]*)\.html$ /test.php?cs1=$1&cs2=$2&cs3=$3&cs4=$4 [L]
In the file test.php I call for the website config files like this:
include('files_include.php');
Now the problem. As soon as I rewrite the URL to a location different from the root one, I get a really strange issue. The page still renders correct in browser but:
Problem 1. I have to replace src="images with src="../images if I want to see the image correct. This can be easily corrected by giving an absolute link, it is the easier part to do.
But the question is why is the relative path changing? Is .htaccess making the browser think we are in search'/ folder? The answer to this question will help me to identify the main issue, which is Problem2.
Problem 2. Sitemaps generators cannot follow the links on the page once the URL is rewritten, as if it appears blank to them, no matter that in browser all looks fine.
Therefore I am guessing that by rewriting the URL to search/A-B-C-D.html I am breaking something with the inclusion of files_include.php.
Basically, I need a general idea of were to look at and the things I should have in mind when rewriting root/test.php to root/search/A-B-C-D.html
Any suggestions?
Your browser is clueless about 'pretty' and 'ugly' urls. It just requests a folder or a file. If you request http://example.com/search/A-B-C-D.html, to the browser you are requesting a page A-B-C-D.html in the /search/ folder. If you have any relative urls on that page, it will request them relative to that /search/ folder. The browser has no clue, and should have no clue, what the internal representation of a request looks like. Heck, at your end of the line it might even be translated to instructions for a colony of hamsters, which will then send correct data through. The browser doesn't need to know how hamsters behave ;-)
The first problem is easily resolved by making your urls absolute. I wouldn't recommend making them relative to the pretty url. An alternate solutions would be to add the <base> tag to the <head> tag of your page. The href property of this tag will be used as a base for any relative links on your page. See mdn for more information. You would then do:
<head>
<base href="/">
</head>
As for your second problem, the include itself is not the problem. include(..) will first try to find the file in the include_path, and otherwise in the script's directory and the working directory. This doesn't change if you create pretty urls. Apache, and php, still know where the actual file is located you are executing. If an include statement fails to load a file it will generate an error too, which is another way you can tell if the include itself is the problem. See the documentation.
But the question is why is the relative path changing? Is .htaccess making the browser think we are in search'/ folder? The answer to this question will help me to identify the main issue, which is Problem2.
It's changing because the browser is loading /search/something-something-sometrhing-something.html instead of /test.php. The first URL has a relative URI base as: /search/ and the second URL has a base of /.
For the second problem, you could try externally redirecting, but not sure if that'll help the sitemap itself, it depends on the generator. Try adding this rule:
RewriteCond %{THE_REQUEST} \ /+test\.php\?cs1=([^&]*)&cs2=([^&]*)&cs3=([^&]*)&cs4=([^&\ ]*)
RewriteRule ^ /search/%1-%2-%3-%4.html [L,R]

Apache URL rewriting keeping old directory location?

So I'm trying to use url rewriting to simplify url's on a website.
Example usage:
www.example.com/test -> www.example.com/index.php?page=test
www.example.com/test/x -> www.example.com/index.php?page=test&value=x
I use this for the rewriting:
RewriteEngine on
#Simplify url
RewriteRule ^(\w+)/?$ index.php?page=$1
RewriteRule ^test/(\w+)/?$ index.php?page=test&value=$1
The issue is that this approach seems to preserve the location prior to the rewrite. So other files loaded on the website like CSS are relative to the original location rather than the location after being rewritten.
Example:
www.example.com/test/x rewrites to
www.example.com/index.php?page=test&value=x.
But CSS files loaded when a user enters www.example.com/test/x are loaded relative to the /test/ folder rather than the / folder. So they're not found.
Am I doing something incorrectly? I'd assumed that rewriting would literally redirect, so things like this wouldn't be an issue. I'd like to solve this issue rather than just using absolute url's for everything - so I can still use it on my test server.
It's important to keep in mind that rewriting is not the same as redirecting. The browser doesn't know about the rewrite that is happening; it just sees the folder structure as it seems.
Relative URLs for site resources are resolved by the browser. So, if you access www.example.com/test/x, and the browser sees <link href="style.css">, it naturally reads this as www.example.com/test/x/style.css, and tries to request this file, only to receive a 404.
One common solution is to always use absolute URLs like www.example.com/style.css. You would most likely store your site's URL as a constant and use <link href="<?php echo SITE_URL; ?>/style.css">.
1)
There is nothing wrong using absolute URLs. You can use define.
For the development environment use:
define('BASE_URL', 'http://test.server.local/');
then for the production environment just change it to:
define('BASE_URL', 'http://www.example.com/');
On all the pages of Your code, You can access those URLs as
x page
So You don't need to change code on all the pages where You reference the BASE_URL
2)
It is a good idea to place all your css in the styles/ directory, then in .htaccess file you can exclude it from rewriting like that:
RewriteEngine on
# add this line:
RewriteRule ^/?styles/.+$ - [L]
#Simplify url
RewriteRule ^(\w+)/?$ index.php?page=$1
RewriteRule ^test/(\w+)/?$ index.php?page=test&value=$1
UPDATE (regarding Your comment)
If you use the concept of BASE_URL the correct URL is made on server and then passed to the browser. If you use <base> you depend on the client side (browser the user uses). It is a good practice to use BASE_URL on the server side, thus you won't depend on the client's browser.
Check out this answer: Is it recommended to use the base html tag?
You can also include the php file (that has the define() function) to all your pages, thus there is no need to use <base> on every page. Here is a nice example of using this.

Categories