Bug with mod_rewrite and PHP? - php

I've run into a bug and I'm not sure of its source. I use mod_rewrite for the following:
RewriteRule ^stuff$ index.php?page=stuff [L]
and it works just fine, but when I use
RewriteRule ^(.+?)$ index.php?page=$1 [L]
CSS is no longer applied to the page and the code doesn't seem to read GET requests.
Can someone elaborate on why this happens for me?

Add a rewrite condition to not rewite if the file is present & use the QSA flag to allow appended parameters:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?page=$1 [L,QSA]

Since your query strings have been converted into a directory-like structure, apache will check the directory if you are not using any path disclosure.
So if you're include the css file like so:
<link href="skins/style.css">
You'll need to disclose the full webpath to the file:
<link href="http://site.com/skins/style.css">`
Also, for all images in your css file you need to do the same, include the full webpath to avoid and issues when rewritting in the future.

Related

fix paths with pretty urls?

I am trying to use pretty links with my website, but i have a problem with paths of css and js files.
Thats what i wrote in .htaccess file
RewriteEngine on
RewriteRule ^register register.php [NC,L]
RewriteRule ^login login.php [NC,L]
RewriteRule ^settings settings.php [NC,L]
RewriteRule ^logout logout.php [NC,L]
RewriteRule ^profile/([0-9a-zA-Z]+)$ profile.php?u=$1 [NC,L]
The problem here appears when i try to access profile.php with the variable
example: http://example.com/xxxx/profile/ashraf
This here will consider profile as the main folder when importing style files which is not a folder, it's just profile.php
Thank you
I think you can add this before your rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Basically what this ( common on many CMS systems ) says, is if Not a real file !-f, or a real directory !-d then continue on. Therefore if the file actually exists, such as a css file, it will not pass the condition and will not be re-written.
So I would say put it right here
RewriteEngine on
## insert here ##
RewriteRule ^register register.php [NC,L]
You may have to place it before each rule, that I am not that sure of, as I haven't done a whole lot with .htaccess in like 5 years. Basically when I learned how to use the URI instead of a URL and route everything through a index.php and a router script.
This is the extent of my .htaccess files these days ( just FYI for my explanation of my lack of remembering ) and it literally never changes. That's one of the biggest benefits of building a router and using the URI
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
This answer I did has a pretty good ( if I say so ) explanation of how what I call the URI method works.
How to change the name of the directory in url in php using htaccess?
Here is another answer I did on this topic that outlines how to build a basic router
Oop php front controller issue
Anyway hope that helps.
The browser cannot know if the paths in the URL are real directories or just parts used in rewrite rules. When it sends a request to the web server to get the CSS, the browser must convert it any relative urls to absolute and assumes the current page is in a path of directories.
In other words, in your HTML page, always use absolute urls: <link href="/xxxx/style/mycss.css" ...>
Put this after the profile.php rule. This will take out the extra 'profile'.
RewriteRule ^profile/(.*)$ $1 [L]
Or use a long path to the css file:
<link href="/xxxx/style/style.css" rel="stylesheet">
Either one should work.

url re write issue in .htaccess file

Here is my problem
in my website base url am redirecting to an inside folder
eg: http://example.com/
to
http://onlinevyapari.com/business/ by using this code in .htaccess file
RewriteRule ^(/)?$ /business/index.php [L]
now I want to use search engine friendly url for the same website my query is like this
http://example.com/business-details.php?id=106
but I want to keep my url like this
http://example.com/business-details/106
I have done in different way like bellow
RewriteEngine on
RewriteRule business-details/id/(.*)/ business-details.php?id=$1
RewriteRule business-details/id/(.*) business-details.php?id=$1
its happening but css is not loading properly
it will be really appreciable to me if somebody help.
thank you
Have your rule like this:
RewriteEngine on
RewriteRule ^(/)?$ business/index.php [L]
RewriteRule ^business-details/(\d+)/?$ business-details.php?id=$1 [L,QSA,NC]
For solving css/js/image path issues just 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 /.
Alternatively you can try adding this in your page's HTML header: <base href="/" /> so that every relative URL is resolved from that URL and not the current URL.
You need to add the following, before your rules, to make the rewrite rules ignore file and directory names.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

remove php extension using .htaccess

I'm working on script, and want to set links to be as following:
www.mysite.com/sign-up.php
to
www.mysite.com/sign-up
and
www.mysite.com/profile.php?username=abc
to
www.mysite.com/profile/abc
I found this code and works for me.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
but when I want to access:
www.mysite.com/profile/abc
The stylesheet doesn't work, it seems the path for it has changed. Also all links in that profile become like:
www.mysite.com/profile/profile/filename.php
It keeps adding /profile everytime
how to fix this?
EDIT: all files works fine with stylesheet except profile.php
You can create it with htacess and modrewrite of apache.
Just check the link below for generating dynamic URL. It might solve your problem:
URL Generation
Have you tried this?
RewriteRule ^(.*)$ $1.php
Just replace you current rule with this one
This isn't a problem with your rewrite rules, it's a problem with your href's.
If you have a style tag like... <link rel="stylesheet" type="text/css" href="css/main.css" media="screen" /> and an a tag like <a href="profile">, they will be treated as relative URLs since there is no forward slash at the start. If your browser is at /profile.php, it will load /css/main.css and the link goes to /profile, but once you are at /profile, the browser thinks you are in that "folder", so it will now look at /profile/css/main.css and your link will go to /profile/profile. You need to make the href's absolute paths, i.e. href="/css/main.css" and href="/profile".
If your CSS link is broken amongst other things, try using the base tag in your document head:
<head>
<base href="http://www.mysite.com/" />
</head>
I have found this to work when dealing with pesky URL rewrites.
There may be better ways to solve it via .htaccess, but this might be a nice short term fix until that happens.
www.mysite.com/sign-up.php to www.mysite.com/sign-up
RewriteEngine on
RewriteRule ^(.*)$ $1.php [r=301,L]
It is required to store this file to folder www.mysite.com/profile/.htaccess
RewriteEngine on /profile/
RewriteRule ^(.*)$ profile.php?username=$1 [r=301,L]
www.mysite.com/profile/abc to www.mysite.com/profile/profile.php?username=abc
The problem is that, first script will redirect string to string.php so www.mysite.com/profile/abc to www.mysite.com/profile/abc.php or www.mysite.com/profile/profile.php?username=abc.php. We must create an exception:
RewriteEngine on
RewriteRule ^profile/(.*)$ profile.php?username=$1 [r=301,L]
RewriteRule ^(.*)$ $1.php [r=301,L]
We can create the whole script as an exception. This will simple do all the work.

.htaccess File, path is redirected properly but images and css are not loaded

Hi I am using the following lines in my .htaccess file
Options +FollowSymLinks
RewriteEngine on
RewriteBase /coaster/CoasterInsider/
RewriteRule signup$ index.php?page=signup [L,NC]
RewriteRule login$ index.php?page=loginHandle [L,NC]
RewriteRule u/(.*)$ index.php?page=profile&username=$1 [L,NC]
The path is getting redirected properly, but the css,flash and images are not loading. Also if I use the following '/' after any url say,
RewriteRule signup(/?)$ index.php?page=signup [L,NC]
It's not finding any page there, i.e. error 404. I just want my htaccess file to work for both /signup and /signup/
When browser displays:
http://example.com/coaster/CoasterInsider/signup
http://example.com/coaster/CoasterInsider/signup/
and encounters a relative URL such as:
<img src="site/images/photo.png">
It translates the relative URL to (respectively):
http://example.com/coaster/CoasterInsider/site/images/photo.png
http://example.com/coaster/CoasterInsider/signup/site/images/photo.png
You should use absolute URLs for assets. Make this a habit:
<img src="/site/images/photo.png">
Alternately you can use the HTML base tag in your pages which tells browsers how to treat relative URLs. Personally I do not recommend it.
This was advised to, and worked for me, place it inside the <head> tags in your html: <base href="/"> Again, it worked for me, I hope it helps someone else.
Use
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
to exclude real files and directories from being rewritten.
You could also add
RewriteCond %{REQUEST_FILENAME} !-l
to do the same for symlinks.

.htacces css/js subdirectory issue

I am trying to make some changes to .htaccess file of a site. I have searched all over the web, but I cant find a clear solution for that, or there is possibility that I have got it wrong. So here we are are:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^/$ /e/www/ [R]
RewriteRule ^news$ /news.php
RewriteRule ^news$ /news.php
RewriteRule ^resources$ /resources.php
RewriteRule ^lexicon$ /lexicon.php
RewriteRule ^contacts$ /contacts.php
RewriteRule ^analytics$ /analytics.php
RewriteRule ^([0-9]+)-([0-9]+)-([0-9]+)-(.*)$ /news.php?yy=$1&mm=$2&dd=$3&alias=$4
RewriteRule ^resources$ /contacts.php
RewriteRule ^articles_search$ /articles_search.php
RewriteRule ^articles_data_search$ /articles_data_search.php
RewriteRule ^profile/([0-9]+)&commit=(.*)$ /profile.php?id=$1&commit=$2
RewriteRule ^profile/(.*)&commit=(.*)$ /profile.php?alias=$1&commit=$2
At last 2 rows, i am trying to have a result from this:
www.example.com/profile.php?alias=kokazani&commit=Edit
to this:
www.example.com/profile/kokazani&commit=Edit
The problem now is the wrong path of css and js files. I need a solution, or a better approach of rewriting rules. I think that a solution ,maybe, is to rewrite everything like css/blabla.css to root directory/css/blabla.css, but if there a standard-solution foro this problem, i would like to know :)
Thanks in advance.
There are 2 possible (simple) solutions:
Use absolute paths for your CSS files (/include/css/style.css etc)
Use HTML's <base> element to set the correct base URL for relative URLs to follow (rather then the directory of the document which is altered by Apache).
My approach would be (as suggested by Truth) using absolute paths. If this is not possible, you could use:
RewriteRule ^(.+)/css/(.*)$ /css/$2
But be warned: This means the client (browser) will end up getting and caching multiple copies of the same file because it is fetching it (as far as it can see) from different paths. Therefore you lose the advantage of client-side stylesheet caching.

Categories