Good day!
I have two scripts in my root folder:
/index.php
/home.php
Both of them receive get parameter and display different pages, f.ex:
index.php?page=info
home.php?page=about
1) When I try to do clean URI by this:
RewriteRule ^([a-zA-Z0-9]+)$ index.php?page=$1 #working ok
RewriteRule ^([a-zA-Z0-9]+)/$ index.php?page=$1 #doesn't load css,js
I found three advices: rather apply path of css,js folders to htaccess, use tag or use absolute path.
I'm wondering are there any other ways to make it solely by htaccess. Or is it possible to change query string the way to remove "/" if there is one f.ex:
I insert
example.org/info/
And my string automatically becomes
example.org/info
2) My htaccess only works with index.php. How should I change my rule to work with home.php if there is "userhome" in query string to make such queries possible:
example.org/home/about
P.S
RewriteRule ^home/([a-zA-Z0-9]+)$ home.php?page=$1
Working the way I need, concerning my second question. However I haven't still done with the problem of css, js loading. Any tips please
The best way to handle CSS not loading is to use <base> tag. That way you can use relative URL's and it won't break anything with your rules.
This will go in the head section of your site..
<base href="http://www.example.com/" />
You can try these rules for your your requirements.
#Redirect to home.php
RewriteRule ^home/([a-zA-Z0-9]+)$ home.php?page=$1 [L]
#Remove the trailing slash
RewriteRule ^([a-zA-Z0-9]+)/$ /$1 [R=301,L]
#Redirect to index.php
RewriteRule ^([a-zA-Z0-9]+)$ index.php?page=$1 [L]
Related
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.
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
I need to rewrite only 1 specific URL, to display to visitors specific content: I tried something like, this:
RewriteEngine on
RewriteCond %{REQUEST_URI} example.com/test/2_5/page.html
RewriteRule ^(.*)$ example.com/tt.html [R,L]
I need to rewrite all requests to:
http://example.com/test/2_5/page.html
to
http://example.com/tt.html
how to do this?
thanks,
Redirect /test/2_5/page.html /tt.html
Ok, mod_rewrite
RewriteRule ^/test/2_5/page.html /tt.html [L]
Remove first / if using .htaccess in the site's root folder, not the .conf file. And, typically, the final url should be the full one, with http:// and domain, but this one will work too. If you want to do everything by the rules then
RewriteRule ^/test/2_5/page\.html$ http://example.com/tt.html [L]
I have a problem (it's probably a simple one but I've never had the need to write regex)
A SEO specialist told me to make pretty URLs so I did with the .htaccess file the CMS provides.
But now he requires me to redirect the old URLs to new ones.
This doesn't work
RewriteRule ^index.php?page=kontakt$ /kontakt.html [R=301,L]
and also this (wich was supposed to redirect to the main page from the index.php file)
RewriteRule ^index.php$ / [R=301,L]
has resulted in sitename.com/?page=kontakt, so now I also have to redirect this.
How do I fix this?
RewriteRule only matches the base URL without the query string. You need an additional RewriteCond for it to work.
RewriteCond %{QUERY_STRING} ^page=kontakt$
RewriteRule ^index.php$ /kontakt.html [R=301,L]
EDIT:
Apparently query string gets preserved in this case, so you're probably getting /kontakt.html?page=kontakt
To discard original query string you need to put ? after URL.
RewriteRule ^index.php$ /kontakt.html? [R=301,L]
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.