remove php extension using .htaccess - php

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.

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

Access relative CSS/JS files with htaccess rewrite rule

I was recently asked if I could make my friends server address more user friendly. His current urls looks like this:
http://wwww.example.com/site/index.php?page=home
http://wwww.example.com/site/index.php?page=about/john
http://wwww.example.com/site/index.php?page=portfolio/concept-art/2013
He would like them to look like this
http://wwww.example.com/site/home
http://wwww.example.com/site/about/john
http://wwww.example.com/site/portfolio/concept-art/2013
which I thought would be pretty simple so I wrote this following rewrite.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !/(admin|css|fonts|ico|include|js)/
RewriteRule ^([^_]*)$ index.php?page=$1 [L]
which seems to work for the basic links like
http://wwww.example.com/site/home
but for something like this
http://wwww.example.com/site/about/john
none of the css or js will load. So I fixed that for now by making all of the files absolute paths but I am worried that my friend is going to add a new plugin or something and forget that he has to make it an absolute path.
My Question
Is there something I could change or add in my htaccess file to get the css and js files to load with a relative path? If so what would I need to do?
Better to use this rule:
RewriteEngine On
RewriteBase /site/
RewriteCond %{REQUEST_FILENAME} !/(admin|css|fonts|ico|include|js)/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?page=$1 [L,QSA]
Then for css/js/images better to 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 header:
<base href="/" />
OR
<base href="http://domain.com/site/" />
Is there something I could change or add in my htaccess file to get the css and js files to load with a relative path? If so what would I need to do?
You could just add the proper relative URI base in your page header:
<base href="/site/" />
Or you could brute force redirect them using mod_rewrite (not preferable):
RewriteRule ^(.+)/(admin|css|fonts|ico|include|js)/(.*)$ $2/$3 [L]

.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.

Bug with mod_rewrite and 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.

Categories