I want to create shorter links for my site, eg
site.come/u/1
instead of
site.com/user.php?u=1
I've had a play with mod_rewrite but quite honestly have no idea what I'm doing with it, I can create the url but when resources are being loaded on the page, they're being loaded from /u/css/core.css instead of /css/core.css
I'm loading in css/images/js with relative URLs, is there any way to make a rewrite rule exclude certain folders without having to change all of my code to absolute urls?
Thanks
You could try with:
RewriteCond %{REQUEST_URI} ^(css|js|images)/.*$ [NC]
RewriteRule ^(.*)$ $1 [L]
And place it before any other rule.
I've tested it here and it works!
Well, you are pretty much answering your own question - instead of using relative path, you will need to use absolute path for your css.
Your browser now sees the page as www.site/u/user.php, i.e. you would need to use ../css/core.css. If your site is in the root, /css/core.css might suffice but it is much safer to use the full path (i.e. echo $webpath."css/core.css" where $wehpath will most likely be something like $webpath="http://".$_SERVER["SERVER_NAME"].$_SERVER["REQUEST_URI"];)
You should use absolute paths and everything will be fine.
For example you can do it like that:
<?php
$baseURL = basename($_SERVER['SCRIPT_FILENAME']);
?>
<link href="<?php echo $baseURL; ?>/css/core.css" rel="stylesheet" type="text/css" />
Related
I need to redirect the following paths to accessPoint.php, but within accessPoint.php I will need to include/require the redirected file which was requested, can this be done?
.htaccess is within the root ./forms/.htaccess.
./forms/form1/index.php
./forms/form2/index.php
./forms/form3/index.php
./forms/form4/index.php
./forms/form5/index.php
The above will redirect to
- ./accessPoint.php
What I have now:
RewriteEngine on
RewriteCond %{REQUEST_URI} /*/index.php
RewriteRule ^.*$ /accessPoint.php [R=302,L]
Current accessPoint.php, just testing by attempting to include one of the files, however nothing gets displayed:
<?php
echo 'access point';
include './forms/form1/index.php';
Edit
Turns out what I have is working, however the files within the included file are now looking within the wrong folder, is there a solution to this?
For example the following:
<link href="css/structure.css" rel="stylesheet">
Is looking within: http://web-forms.localhost/css/form.css
When it should be looking within: http://web-forms.localhost/forms/form1/css/form.css
What you are describing is correct. When you include a php file as you show, it doesn't go through the web server but rather just reads the local file system. I suspect your RewriteCond is incorrect. Try this, for example:
RewriteCond %{REQUEST_URI} "\/index\.php$"
EDIT: To the update in the question. The way to fully deal with the situation is to use absolute paths, for example
<link href="/css/structure.css" rel="stylesheet">
Or, better yet, use php to determine what to include:
<?php
$BASE_PATH = '/';
echo "<link href=\"{$BASE_PATH}css/structure.css\" rel=\"stylesheet\">"
?>
?>
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]
I have this htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ dl.php?id=/$1 [QSA,L]
</IfModule>
it works perfect. I just have one problem, I insert my site urls and images like these
<img src="img/image.gif">
<a href="do.php">
its short url not full like
<a href="mysite.com/img/image.gif">
so when now I open 127.0.0.1/dl.php/1/ I can get
$_GET['id']
But my links and images want to open from
http://127.0.0.1/dl.php/img/h.png
but it must be
http://127.0.0.1/img/h.png
can anyone help me with this? I can't change all urls in my site and make them full url. I have like 50 page and I want this htaccess just for dl.php file
It wants to do this because you're using relative paths in your src.
Simply prepend a / to your paths, and it will go to the root of your site (ie. relative to the domain), rather than relative from the current folder (of course, not a real folder, but according to the URL your browser doesn't know any better).
<img src="/img/image.gif">
<a href="/do.php">
Sorry to say but you will need to change each path. There is one alternative, but you'd still have to change every path unless dl.php contains like a header/footer. In this case, you can append a <base href=".." /> tag to the header, which will force relative paths to be resolved relative to the path you give it.
<head>
...
<base href="http://www.mysite.com/" />
</head>
If I have a link, for example: dashboard/xyz/fff/ and I use modrewrite to change it to dashboard/?loc=xyz&action=fff when the page loads are loc and action available as variables to use?
If so, then here's a specific example I can't seem to get to work. My rule as it sits:
RewriteRule ^getclients/([a-z\-]+)$ /dashboard/?action=getclients&module=$1
And the link that is sending them to that url:
<li>SEO Analysis</li>
I want now to be at .com/dashboard/?action=getclients&module=$1 and use those variables to load the page content that's needed.
However: Now the page redirects to what I believe is the "right page" but the CSS is all broken. I only have plain text. Feel free to suggest another way to achieve the same effect as well perhaps using jQuery and Ajax or something to load up sections of the site.
Thanks!
If you don't want to use absolute paths, you can try rewriting the requests for images, javascript, and css. Maybe something like this:
RewriteCond %{REQUEST_FILENAME} \.(js|css|png|jpe?g|gif)$ [NC]
RewriteRule ^getclients/(.+)$ /dashboard/$1 [L]
Make sure that the file locations are exact, or the server might send the data from the wrong relative directory.
For loading css, images, js files properly from a different relative path you should specify a base URL for all relative URLs on a page like this:
<base href="http://www.example.com/dashboard/" />
I have successfully created rewrite rules to handle URLs like
http://example.com/xyz
http://example.com/xyz/
http://example.com/xyz/abc
http://example.com/xyz/abc/
Here is the mod rewrite & regex:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([0-9]+)(?:\/)?$ /index.php?p=$1 [L]
RewriteRule ^/?([-a-zA-Z0-9_+]+)(?:\/)?$ /index.php?n=$1 [L]
RewriteRule ^/?([-a-zA-Z0-9_+]+)/([-a-zA-Z0-9_+]+)(?:\/)?$ /index.php?n=$2 [L]
This works great, but the problem is that when I go to http://example.com/xyz/abc, my script gets the correct input (n=abc) but the paths are messed up, so all relative URLs in my code are broken, as they are now relative to xyz instead of the root.
Is there a way around this at all? As you can see above I have tried to redirect to /index.php to force the path to be correct. My brain is fried after a long day of regex and code, so I hope it's not something disastrously trivial.
Use the HTML base element to force all relative URLs to use a different path than the one the browser displays.
<base href="http://www.example.com/"/>
...
link
The relative URL will use "http://www.example.com" as its base, even if the browser thinks it's looking at the page "http://www.example.com/xyz/". So the link goes to "http://www.example.com/relative/url" instead of "http://www.example.com/xyz/relative/url"
And there's the Stack Overflow way of doing it, in which every URL is either an absolute path (to resources like images) or paths including the root (i.e. "/feeds/questions/123"), which avoid the relative path issues.
The three options are:
1) Use a base tag (this will affect every relative URI on your page, including links, images, scripts, stylesheets and so on)
<base href="http://yoursite/">
2) Change all of your links to fully qualified URIs
<a href="http://yoursite/yourpage.html">
3) Use the "/" prefix to show that the path is relative to the root on each URI.
<a href="/yourpage.html">
I have personally used the base-tag option the most, which does get some bad press (from people that have used it without really understanding it). When it comes to mod_rewrite, the base tag is perfect as you probably DO want all your paths to be relative to the root, including all your images, css, scripts and links.
Edit: I assume you are talking about URLs in your HTML code, e.g. to images and stylesheets, that are broken.
Nope, as far as I know there is no way around it, because the browser sees a path, and requests resources relative to it. It has nothing to do with the server, and there is nothing you can do.
You will either have to resort to a different splitter that is not interpreted as a directory splitter (e.g. underscores), or use absolute paths, or use the <base> tag. I have never used the base tag myself, however, and it is not very well regarded wherever you look. The best thing would probably be to switch to absolute paths.
modify your last RewriteRule to include the path. you have the path in the regular expression.
RewriteRule ^/?([-a-zA-Z0-9_+]+)/([-a-zA-Z0-9_+]+)(?:\/)?$ $1/index.php?n=$2 [L]
or alternatively (depending on what you are trying to achieve):
RewriteRule ^/?([-a-zA-Z0-9_+]+)/([-a-zA-Z0-9_+]+)(?:\/)?$ /index.php?n=$1/$2 [L]
I tried using the BASE element in my pages as a shortcut instead of changing all urls. Add the base element as follows:
<base href="/">
And here are the results:
This: <link type="text/css" rel="stylesheet" href="my.css">
Becomes: <link type="text/css" rel="stylesheet" href="/my.css">
This: <script type="text/javascript" language="javascript" src="include/my.js"></script>
Becomes: <script type="text/javascript" language="javascript" src="/include/my.js"></script>
This: <a href="foo.html">
Becomes: <a href="/foo.html">
This: <a href="foo/bar.html">
Becomes: <a href="/foo/bar.html">
You can always override the base tag where necessary:
This: <a href="/foo">
Remains: <a href="/foo">
This: <a href="/foo/bar/">
Remains: <a href="/foo/bar/">