In my .htaccess file i have the following re-write rule
RewriteRule ^([a-z]*)$ work.php?album=$1 [L]
I am reading all directories in root directoy and creating links like
code in PHP
$dir=glob('images/works/*');
$dir_listing=array();
foreach($dir as $list)
{
if(is_dir($list))
$dir_listing[]= (basename($list));
}
foreach($dir_listing as $folders )
echo"<a href='$folders' ><img src='Default-thumbnail-folder.jpg' /> </a>";
the problem is i have three folders under works directory {alb,ban,bas}
the first and last {alb and bas} work properly for my redirect rule URL{siteName/works/bas}
but the second {ban} creates a URL like {siteName/works/ban/} Notice the extra "/" is creating the problem, it seems for some folder names it is creating the problem, so please help me in creating a proper redirect rule
i want a link like
<a href="alb" >About</a> // to redirect to work.php?content=about
I also notice that if URL in browser is like work/alb/ CSS is not applied
i tried
<link href="sample.css" rel="stylesheet" type="text/css" />
and
<link href="css/sample.css" rel="stylesheet" type="text/css" />
it does not work for URL like {works/about/} it works for {works/about}
Easy!
Try to change your rewriterule to:
RewriteRule ^([a-z]*)/?$ work.php?album=$1 [L]
So that the "/" at the end may or may not be present, in both cases it's ok.
to avoid rewriting css URI's use these rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
as for your other problem you can use a trim on the result url to make sure you never have a "/" at the end
trim($url,'/');
Related
I am trying to convert my dynamic PHP link to a clear link; for that I used RewriteRule. But the work seems to be not done properly
I am trying to convert
dappersole.in/product.php?product_id=45;
To
dappersole.in/product/46;
Using
RewriteRule ^product/([0-9]+)$ product.php?prodct_id=$1 [NC,L]
After this the page does not load properly. But if I remove product/ from RewriteRule, the page loads perfectly
RewriteRule ^([0-9]+)$ product.php?prodct_id=$1 [NC,L]
dappersole.in/45;
Your page won't load fully because you are referring to your CSS and JavaScript resources relative to the current location. You need to change this to refer to the resources relative to the root of the site. For example, change this:
<link rel="stylesheet" type="text/css" href="css/slider-pro.min.css" media="screen"/>
to this:
<link rel="stylesheet" type="text/css" href="/css/slider-pro.min.css" media="screen"/>
Note the leading slash before css/slider.... You need to do this for all your CSS, jQuery and image resources. Alternatively, you can save time by adding the following to your <head> node:
<base href="/">
Or, even better:
<base href="http://dappersole.in/">
Additionally, I recommend you do the same with your mod_rewrite rule:
RewriteRule ^product/([0-9]+)$ /product.php?product_id=$1 [NC,L]
Lastly, I recommend you ensure Multiviews is not enabled, by adding this line to the top of the file:
Options -Multiviews
I too have had similar issues to this with Apache 2.2 - if the name and the rewritten name are very similar it fails. You could cheat slightly and use a different url such as http://www.example.com/item/46
RewriteRule ^item/([0-9]+)$ product.php?product_id=$1 [NC,L]
I've had that same problem on an older version of apache / mod_rewrite and I never found out what the exact problem was. But it definitely did not like rewriting /something/123 to /something.php?....
The quick fix I used, was renaming my php file to _something.php and using that.
So in your case renaming product.php to something that does not start with product like _product.php:
RewriteRule ^product/([0-9]+)$ _product.php?product_id=$1 [NC,L]
^ strangely enough this solved my problem...
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 been wondering on the internet on how to work with htacces(really hard to learn it). And when I was lurking in the internet, I found this: http://www.generateit.net/mod-rewrite/
Well, I inserted my url(working on localhost):empresa.com/index.php?p=sub_artigo&id=1&cat=Mercearia
and it gave me this(with all options by default):http://empresa.com/sub_artigo/5/Mercearia.html
And the .htacces code was this:RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /index.php?p=$1&id=$2&cat=$3 [L]
And when I generate the url in php I do
$response2[nome_sub_artigo]
And then, when I click the button, it appears like, only html.
example: http://s14.postimg.org/wr137fx4x/htacces_error.jpg
Any idea what is happening ?
It looks like you are using relative links for your assets (images, javascript, css).
That means that when you look for css/my_stylesheet.css, from the new url, the browser will request a url like http://empresa.com/sub_artigo/5/css/my_stylesheet.css.
The easiest solution is to always use absolute urls for your assets, like /css/my_stylesheet.css, etc.
Your PHP code is outputting relative paths to the style sheets.
Example:
<link rel="stylesheet" type="text/css" href="styles.css">
Given your example input URL, this causes the browser to look for a stylesheet at the following URL:
http://empresa.com/sub_artigo/5/styles.css
This is because the browser doesn't know that the URL has been rewritten - it believes it is viewing a file in a subdirectory that doesn't really exist.
Instead you should use an absolute path, such as:
<link rel="stylesheet" type="text/css" href="/styles.css">
Notice the leading / on the path? This will tell the browser to look from the root of the domain:
http://empresa.com/styles.css
In this way you can still decouple your HTML from the protocol and domain/port (so you aren't tied to http://empresa.com) but the path will always be the same regardless of the URL that was used to reach the referencing page.
Try this code :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /empresa.com/index.php?p=$1&id=$2&cat=$3 [L]
Problem: rewrite url causing some links to break.
.htaccess has below rule:
RewriteRule ^blog/([0-9]+)/[-0-9a-zA-Z]+$ index.php?action=blog&postID=$1\%23disqus_thread [NC]
Style sheet reference in header template:
<link rel="stylesheet" type="text/css" href="style.css" />
I can click on:
domain.com/blog/1/title-of-article and get to the file just fine, but style sheet link breaks
If I go directly to:
domain.com/index.php?action=blog&postID=1#.UYV1mcqRiSo then the style sheet loads fine (ignore #.UYV1mcqRiSo, that is code from Disqus).
This is also breaking my logo link, which is:
<a href="./">
Instead of taking me to domain.com, it's going to domain.com/blog/1/
My basic file structure is:
index.php and style.css is in root, which loads up viewPost.php in/templates folder.
What is going and how do I correct this?
1. About the stylesheet link
You have to include these conditions in your .htaccess before the rewrite rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
The first means that rewrite rules work only if the requested URL is not to an existing file, and the second does the same for existing directories. Without these conditions the request for style.css is also passed to your index.php file, not to the style.css file directly.
If you have these rules already and it still doesn't work then the problem is with URL paths, see below.
2. About URL paths
You're using relative URLs in your links, both in the <a href="./"> and in the <link> tag. In this case they are resolved to the current directory of the URL (it doesn't matter that it's rewritten to the same file serverside, the path is prepared on the client side and every part separated with a slash / is treated as a directory. Thus if the current URL is domain.com/blog/1/, ./ is resolved to domain.com/blog/1/.) The path to style.css may be resolved to domain.com/blog/1/style.css. If you want these links work as if they're directly after the domain name in the URL, you have to use absolute URLs, i.e., / without the dot for the link on your logo; /style.css for the stylesheet link.
Easiest solution: Just set your links relative to the domain root, by fronting them with a slash (resp. removing the dot referring to the current folder in the link):
<link rel="stylesheet" type="text/css" href="/style.css" />
Logo
I have this in my .htaccess :
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule . /index.php
and this is how i work with the parameters :
<?php
$request = $_SERVER['REQUEST_URI'];
$request = substr($request,1);
$params = explode('/', $request);
$safe_pages = array('page1','page2','page3');
if(in_array($params[0],$safe_pages))
{
include($params[0].'.php');
}else
{
include('404.php');
}
?>
Let's say my site is : www.site.com
www.site.com/page -> works perfectly
www.site.com/page/parameter -> doesn't load any css or images because it looks for them in root/page instead of root/
I tried to use absolute links for my css and images but it still didn't work, so i guess the problem is in the .htaccess or i'm missing something.
The problem lies not within htaccess rewrite rules, but in your html - yuor links to static content (like css, images or scripts) should start with slash to be ralative to the domain. So instead of
<link href="css/style.css" />
<script src="js/script.js"><script>
Use
<link href="/css/style.css" />
<script src="/js/script.js"><script>
Other way is to use base tag but it can break named anchors on your page
Use
<base href="http://absolute_path_here/">
in your HTML before the closing </head>.