I have just one root directory with index.php in it along with two folder img and css.
I refer to files in this folder like:
src="img/path.png" i.e relative to the root directory.
The other day I had some mod_rewrite question & this is what someone gave me, which seems to wrok fine except for trailing slashes and css/img breaking apart
RewriteRule ^$ index.php?page=1 [L]
RewriteRule ^([0-9]+)/?$ index.php?page=$1 [L]
RewriteRule ^([A-Za-z]+)/?$ index.php?category=$1&page=1 [L]
RewriteRule ^([A-Za-z]+)/([0-9]+)/?$ index.php?category=$1&page=$2 [L]
This is what is bothering me:
Using Rule1:
www.example.com changes to www.example.com/index.php?page=1 which is great
Also www.example.com/ some how changes to www.example.com which is again great
Using Rule2:
www.example.com/2 changes to www.example.com/index.php?page=2 like what I would want
But using www.example.com/2/ (TRAILING SLASH) also retrieves page=2 but somehow the img and css breaks apart.
I am guessing the problem is with url being treated as directory structure and then it cant find img and css folder there.
Using Rule 3:
www.example.com/Football changes to www.example.com/index.php?category=Football&page=1 again like what I would want
But www.example.com/Football/ (TRAILING SLASH) suffers from the same problem with img and css breaking apart
Using Rule 4:
www.www.example.com/Football/2 even without the trailing slash breaksdown on css and img however the page can retrieve tha page and category correctly.
How do I correct this problem without having to use absolute paths in my html.
Please advise on the trailing slash problem as well.
Can you not reference your images/css using relative but starting with a /?
/images/blah.jpg
/css/style.css
You could add a rule like:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|robots\.txt|favicon.ico)
# Other rules here
The idea is to avoid the rewrite case for an existing file or directory.
With a trailing slash, the path will be interpreted as a directory. Wherever you include images, css files and such you have to change the paths. Say you have images in an img folder, you probably use it in css like:
background: url(img/image.png)...
Change that to :
background: url(/img/image.png) ...
Same goes for everywhere you have images referenced or other paths. Using a path that starts with "/" means that you are searching from the root directory. The rules you're using are the rules that I'm ussually using and didn't have any problems with them. Actually, I find it easier to build my paths with a starting "/".
Just as a last example, if you have: mysite.com/home and you have <img src="img/image.png" /> the image will be searched in mysite.com/img/image.png which is probably what you want.
But if you have mysite.com/home/ and <img src="img/image.png" /> the image must reside in mysite.com/home/img/image.png which is not what you want, thus using <img src="/img/image.png" /> would be the solution for you.
Related
I decided to use mod_rewrite to make my URLs look better. I created very simple rules:
RewriteEngine on
RewriteBase /
RewriteRule ^(profile|contact|help|events|account|logout)/?$ index.php?p=$1 [NC]
RewriteRule ^home/?$ index.php [NC]
RewriteRule ^PlaceOrder/([0-9]+)/?$ index.php?p=mos&gc=$1 [NC]
It almost works well but it doesn't. So for example, [NC] is supposed to make it case-insensitive. However, for some reason, when I type for example localhost/Help in the browser, for some strange reason, it redirects to home page.
Another issue is with the last rule. If I type in localhost/PlaceOrder/1 it works as expected and opens index?p=mos&gc=1. But after that if I click on, for example, Account button, the browser uses this URL: localhost/PlaceOrder/account. Which is wrong. It should only use localhost/account. Why is it adding that sub-directory in there? It is only happening after the last rule is used. Prior to using the last rule, all links work well.
I'm going to guess that the localhost/Help isn't because of the rules and because of something in your index.php script. There's nothing those rules do that could possibly rewrite Help to the home page.
The second issue is a matter of a relative vs absolute URL issue. You're probably using relative links in your pages and because you've changed your relative URL base (which would be /PlaceOrder/ all relative links will have that prepended to it. You need to either change all your links to absolute URLs (they'd start with a /) or add this to your page's header:
<base href="/" />
I'm creating a new website and it has some php in it. The site basically would work like this, i have /index.php?page=category_page. The the category would be the category and the page would be the sort-of sub category / actual page. The rewritten rule would look like this: /category/page.
I've got this:
RewriteEngine On
RewriteRule ^category/([^/]*)$ index.php?page=$1 [L]
But i dont know how to separate the category and the page, any help?
The second thing is, in my index.php (for instance) i have some css from an external file:
<link rel="stylesheet" type="text/css" href="style/style.css"/>
This doesn't work with the rewrite rule, because it tries to load /category/page/style/style.css which doesn't exist / doesn't rewrite. How do i make it work? I know a simple fix would be to put /style/style.css and that would load from the root of the website, but i'm currently running the website from a sub directory e.g. example.com/new/index.php so that isn't an option. Any help with this?
Problem 1:
You could define multiple parameters in your regex, e.g. category & page (see below).
With such a broad rewrite rule, you would want to add a condition not to rewrite for stylesheets, images, and other assets, though.
I also modified your pattern to only match letters, digits, hyphens, and underscores, which would prevent the use of non-standard characters in your category or page names.
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(js|css|png|gif|jpg)$ [NC]
RewriteRule ^([\w\d\-]+)/([\w\d\-]+)/?$ index.php?category=$1&page=$2 [L]
Problem 2:
You're using a relative url in your href, which is appending the stylesheet's location to the current location defined in your browser (/category/page/).
Even though the server-side is rewriting that URL, the browser is unaware of the rewrite.
If you use an absolute URL instead, your browser will define the URL relative to the BASE url (/).
Try this:
<link rel="stylesheet" type="text/css" href="/style/style.css"/>
Use this RewriteRule
RewriteEngine On
RewriteRule ^(.+)/(.+)/$ index.php?page=$1_$2 [L]
I've googled for some mod_rewrite tutorials, but couldn't figure out how to solve my specific problem regarding the way I created my site with includes.
I've an index.php and in this file there are several includes like index.php?s=events will include events.php and so on.
If I use this code:
RewriteEngine on
RewriteRule ^/?([a-zA-Z_]+)$ index.php?s=$1 [L]
it will result that www.mydomain.com/events just shows the events.php itself, not with the "skeleton/framework" index.php around it, like it should be. So it just loads the specific included file.
My target is to call www.mydomain.com/events for example to show the whole index.php?s=events page.
Try putting slash before URL into the RewriteRule:
RewriteEngine on
RewriteRule ^/?([a-z_]+)$ /index.php?s=$1 [L,NC]
I added NoCase flag to get rid of capital letters in the regexp.
I have some problems with mod_rewrited at .httacess.
We have created a website, the website links have this sctructure.
www.myweb.com/page.php?title=this-is-the-title&filmID=454122
AND
www.myweb.com/video.php?title=this-is-the-title&filmID=2567971&player=veevr
AND
www.myweb.com/gallery/index.php?galeriID=11
is possible to change this structure to:
www.myweb.com/page/this-is-the-title/454122.html
www.myweb.com/video/this-is-the-title/454122/veevr.html
www.myweb.com/gallery/index/11.html
Any help will be really great
Thank you for reading the post!
Best Regards
JoinOG
In your .htaccess file in your web root folder put the following code.
RewriteEngine On
RewriteRule ^page/([^/.]+)/([0-9]+).html/?$ page.php?title=$1&filmID=$2 [L]
RewriteRule ^video/([^/.]+)/([0-9]+)/([^/.]+).html/?$ video.php?title=$1&filmID=$2&player=$3 [L]
RewriteRule ^gallery/index/([0-9]+).html/?$ gallery/index.php?galeriID=$1 [L]
What this does is mask the url /page/something/12345.html to page.php?title=something&filmID=12345. It masks it, so when you go to the first URL it still looks like the first URL in the address bar but is really at the second URL. Simple tutorial on how this works is here: http://corz.org/serv/tricks/htaccess2.php
The server will think you are in the folder /page/something/ so if your CSS, images and hyperlinks are locally relative links they will not work, e.g. it will look in /page/something/yourimage.png for an image linked to like this <img src='yourimage.png'/>. To get it to work as you'd like it to, you'll need to put a forward slash before all your links to make it relative to your website's root folder like this <img src='/yourimage.png'/>.
im still new to .htaccess
so i looked on stackoverflow for my answer and i cant find it. i have rules for my site i had to setup so i got my rules from this:
SEO Friendly URL to Dynamic URL using PHP
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?request=$1 [QSA,L]
but for some reason my css is broken but my php is good
example url:
http://www.domain.com/aboutme
http://www.domain.com/css/version.css
The problem is due to Capitalized file name Version.css.
If your devbox is windows it'll work. Windows is case insensitive.
But when you test it in server (Unix) it will break.
Because Unix is case sensitive
Better rename Version.css to version.css. Its safe for the future
I have a super answer for this problem. I've been looking everywhere for an easy solution that wouldn't make me hardcode baselinks for all my relative paths, like imgs, css, javascript...
So here it goes, add this between the <head> tags of the pages you are having problems:
<base href='http://www.mydomain.com/'>
This will make that your relative path links will start with this base link. Simple as that.
The <base> tag specifies the base URL/target for all relative URLs in a document. Put the <base> tag as the first element inside the <head> element, so that other elements in the head section uses the information from the <base> element.
Did it work for you?