OK guys, I have a problem that is literally driving me crazy. Here's what happened :
I decided that I wanted to rewrite the URL on my web-site.
It is supposed to rewrite from this syntax :
http://www.sample.com/programming.php?name=something
to this :
http://www.sample.com/tutorials/programming/something.php
Or (eg. 2) :
http://www.sample.com/other.php?name=test
to this :
http://www.sample.com/tutorials/other/test.php
So my URL syntax would be :
http://www.sample.com/tutorials/(name of my file)/(name of the variable).php
I have tried the following code :
RewriteEngine On
RewriteRule ^tutorials/programming/(.+)$ /programming.php?name=$1 [L]
RewriteRule ^tutorials/other/(.+)$ /other.php?name=$1 [L]
But, it doesn't rewrite the URL properly. The detailed explanation is below :
So, when I visit my original site, it appears like this :
http://www.sample.com/programming.php?name=something
If I visit this URL :
http://www.sample.com/tutorials/programming/something.php
I get my web-site HTML, but without my CSS layout (just HTML displayed). Also, if I click on any other link on non-CSS site, I get error 404. Note that the URL for the index.php site isn't as it's supposed to be :
http://www.something.com/index.php (Correct index.php URL)
but it's like this :
http://www.sample.com/tutorials/programming/index.php (which does not exist).
I have read over 10 tutorials online, asked my colleague to help me out, but neither did his solutions work. So, all I want to accomplish is that my URL is rewritten, so when the user choose a tutorial in programming, I don't get this URL in the address bar :
http://www.sample.com/programming.php?name=something
but this :
http://www.sample.com/tutorials/programming/something.php
and that is all I want.
I have tried to be as detailed as possible. If you need additional details, please, let me know.
Thanks in advance!
I get my web-site HTML, but without my CSS layout (just HTML displayed). Also, if I click on any other link on non-CSS site, I get error 404. Note that the URL for the index.php site isn't as it's supposed to be :
The relative/absolute paths you have in your page content is getting a different base because of the extra slash. When you have something like <img src="images/blah.gif">, the relative base is derived from the URL the browser sees (not what is internally re-written by the server). So if the browser sees: http://www.sample.com/programming.php?name=something, the URI is /programming.php and the URI base is /. But if the browser sees http://www.sample.com/tutorials/programming/something.php, the URI is /tutorials/programming/something.php and the URI base becomes /tutorials/programming/, which I'm assuming is not where your images/css/scripts/etc are located (since that directory probably doesn't even exist).
You need to either correct the URI base in all of your page headers by adding a:
<base href="/">
Or change all of your relative links to absolute links.
I get my web-site HTML, but without my CSS layout (just HTML displayed)
how did you include the css? did you include your files in the way
../../css/file.css
or in absolute mode
/css/file.css?
to check what really is the fault (i guess your rewrite success in your task eg i didn't understand it the right way) can you give us the real uri's?
Related
I've been struggling with this as I'm not so good in rewriting.
I want a URLs like these:
http://www.example.com/d/page1
http://www.example.com/d/page2
http://www.example.com/d/anythinghere
to always resolve (rewrite) to this:
http://www.example.com/dir.php
or maybe event better to:
http://www.example.com/dir
which in turn should be rewritten to /dir.php
For those who would like to know why is this needed:
I need to have my AngularJS non-single-page-app work without hashbangs where I need my pagination or anything - I want to have distinctive page URLs in order for the Web spiders to crawl my content properly.
So I'm hoping that I will be able, by making such requests resolve always in my page where AngularJS is dir.php to have links: Go to page 3
I'm still not sure if this is going to work at all. Anyway, the purpose of this rewrite thing is to force the server not to go away from this page when such a link is clicked. This just struck me: but it would create at least a page reload, wouldn't it? If so, that's really bad...
RewriteRule ^/d/(.*)$ dir.php/$1
RewriteRule ^/dir/(.*)$ dir.php/$1
First rule will change everything afer /d/ to /dir.php
Second rule will forward everything after /dir/ to /dir.php
on your menu change the link
<a href="dir.php">Your Menu <a/>
to
<a href="dir">Your Menu <a/>
in the. htaccess file try this
RewriteEngine On
RewriteRule ^dir dir.php
Actually, all my previous attempts were valid - the thing is I was getting broken layout so I assumed rewrites weren't completely correct. It turned out including <base href="/"> rectified the thing by forcing the paths to be relative to the root.
I wanted to pass the parameters to my PHP page in a friendly fashion, so here's what I wrote in .htaccess file
RewriteRule ^confirm\/(([a-z0-9]).+)\/(([a-z0-9]).+)$ www.example.com/confirm.php?email=$1&order=$2
its working well, and I can get the parameters in PHP using
explode('/', $_SERVER['REQUEST_URI'])
I have 2 problems though,
1- when I submit this page, I need it to go directly to confirmation.php, instead it submits to www.example.com/confirm/example#mail.com/12-HDYF/confirmaction.php !!! how can this be changed.
2- the page was originally working perfectly, but when I type the friendly URL all jquery files give me the below error, which really seems unrelated.
That is because the client (the browser) sees the page it is browsing as www.example.com/confirm/example#mail.com/12-HDYF/ and when it analyzes it and parses the html, it sees it something like:
<script src="jquery.js"></script>
<form action="confirmation.php"... >
So the browser sees relative urls to the page he is at the moment. So if you tell him to search the jquery.js file in the same folder as the one he currently is, he is going to try to download from www.example.com/confirm/example#mail.com/12-HDYF/jquery.js, and the same thing happens when you submit the form.
To avoid this, use absolute URLs (note the leading slash):
<script src="/jquery.js"></script>
<form action="/confirmation.php"... >
If you have your files in a subfolder of the DocumentRoot, put that subfolder at the beginning of the URL.
Change your url structure to avoid ^confirm for both raw and friendly URL, that should make things easier to debug (and avoid loops). This is a guess since you haven't posted your input that fails.
RewriteRule ^orderconf\/(([a-z0-9]).+)\/(([a-z0-9]).+)$ www.example.com/confirm.php?email=$1&order=$2
I expect the answer is going to be something so simple I'll want to cry, but I can't seem to figure it out. I'm new to mod_rewrite.
I wanted to change my links from things like domain.com/?p=about to domain.com/about*/* (with the trailing slash) and it works fine, but whenever I move on to a link, it appends the new link to the back of the url. For example, I have an about and a contact link. If I click about it navigates to domain.com/about/ then if I click contact, it navigates to domain.com/about/contact/ and will keep adding the links to the end of the url. If I'm at domain.com and click a link(about, in this case) it will go to domain.com/about/ and if I click about 4 more times, my address bar is going to say "domain.com/about/about/about/about/about/" I have reproduced this in a very simple example below, what am I doing wrong?
.htaccess
RewriteEngine On
RewriteRule ([a-zA-Z0-9]+)/$ index.php?p=$1
index.php
about | contact<br><br>
<?php
if(!isset($_GET['p'])) {
echo "home";
} else {
echo $_GET['p'];
}
?>
Thank you for your help!
edit:
It works okay if I use an absolute path, but I'd rather not if I don't absolutely have to.
edit2: adding
RewriteBase /
breaks the links. They appear to be going to domain.com/about/ and .../contact/, but I get a 404 - I'm assuming the rule I used is somehow incompatible with the way I'm doing my linking, which is why I included index.php as well.
You are defining all of your links in HTML relative to the current path.
You will need to change your links such that:
about | contact<br><br>
becomes (note the leading / on the urls):
about | contact<br><br>
When you are on a page site.com/about/us a link like <a href="home/" gets resolved by the browser to be site.com/about/us/home.
The solution is to change all of your links, images, stylesheets, and javascripts to use absolute paths in your URLs, not relative ones like you have now.
EDIT: Just noticed your edit. You really should use absolute paths, not relative ones. If you want to keep the relative URLs then you will have to use something like <base href="/" /> on all of your pages.
Whatever you do, clicking a about will append about/ onto the end of the URL. That's how relative links work.
Your choices are, in order of sensibleness:
Just remove that trailing slash. That's the cause of your problem:
about
A relative link will replace the last section of the path (after the last /) with your new value.
Add a preceding ../. This is a bit hacky, but it lets you keep that valuable trailing slash
about
Do a 301 redirect from /about/about to /about. This will cause the address bar to change from /about to /about/about and back again.
Maybe a bit too late to answer, but adding this one line in the <head> section would do the trick:
<base href="/"> or <base href="your-domain-name">
This is hard to explain, so hopefully I'm understood in my question.
(1) I want to create "SEO friendly" links that remove the query string from a web site. There is only one variable, let's call it "page". Here is the following code for my .htaccess file.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)$ index.php?page=$1
This works in providing the proper redirect. So /applications/ will send to index.php?page=applications.
(2) My index.php will include a view page based on the value of $_GET['page']. Here is some sample code below:
switch ($_REQUEST['page']) {
default:
include ("home.php");
break;
case "apps":
include ("apps.php");
break;
}
There seems to be no problems so far.
(3) Let's make apps.php an exact copy of home.php. home.php loads just fine, but apps.php will not load linked CSS and JScript pages. When apps.php is loaded, it thinks it is in the /apps/ directory. To load the linked pages, I would need to insert a "../" in front of the file name. Then it displays correctly.
So my question is -- How can I properly write the .htaccess file so the home.php and apps.php page can be identical files and produce identical results, instead of the apps.php file being treated as if it were in the /apps/ directory?
First, I should apologize as I don't have a solution which involves making changes in the htaccess. My solutions are of a different nature.
I think the problem can be solved if you have a config variable,preferably in a config file, which will hold the root folder for images, js etc. Most of the time its public_html, the document root, where the url of your website points to. so your config variable could look like:
$base_url = 'http://www.mywebsite.com/';
The config file should be included in index.php unconditionally.
So, when you include any js or images, you do it like this:
<script type="text/javascript" src="<?php echo $base_url;?>js/global.js" />
<img src="<?php echo $base_url;?>images/gradient_green.jpg" />
If you include the config file in index.php, all the files you include based on switch-case conditions, will be able to use the $base_url variable.
Another possible solution is to use the base tag. Look it up here:
http://www.w3schools.com/TAGS/tag_base.asp
I hope this helps.
use absolute urls for js, css and images on your pages (starting with a slash).
/js/main.js instead of js/main.js
You can't do that with .htaccess unless you do an external redirect (by adding the [R] flag to your RewriteRule). But then you expose the query string, which is what you wanted to avoid in the first place.
The reason it can't be done: It is not apps.php which "thinks it is in the /apps/ directory" - it's the browser which "thinks" that. In the page source generated by apps.php, you send relative URLs back to the browser, and now the browser will request these resources relative to the location of the page it asked for. For the browser, the page it got is in /apps/, no matter what rewriting you applied internally on the server side.
So the options you have are:
Do an external redirect with your .htaccess (and defeat your original purpose ;-)
Change the URLs dynamically with PHP while processing apps.php etc, as you said (prefixing ../ to the URLs)
Use absolute URLs, just as #nobody has suggested in his answer.
The last one is the only real option IMHO.
I have a php file that loads an article from a db based on the given variables. There is also an .htacces file in the root of the site. I used this in the htaccess to redirect
RewriteRule
^articles/([a-zA-Z0-9-_\s]+).html$
template/index.php?action=viewarticle&alias=$1
after redirecting, the page shows fine but the html in the page goes wrong, for example:
media/2011/02/21/logos.jpg turns in to articles/media/2011/02/21/logos.jpg
This happens because the htacces is redirecting. Is there anyway to do this redirect while keeping the root dir unchanged?
This happens coz the htacces is redirecting.
No, this happens because the browser thinks that
example.com/articles/my_article.html
is a resource in the /articles sub-directory, and treats all relative URLs as relative to /articles.
There is no way to change that behaviour.
You will need to start using absolute image references, or relative image references that consider the additional directory:
<img src="/media/2011/02/21/logos.jpg"> <------ recommended
<img src="../media/2011/02/21/logos.jpg">
you could also use <base> as suggested by #Boris but absolute paths (or full URLs) are a vastly cleaner solution to the problem in my opinion.
First, what do you mean by "html in the pages goes wrong":
Is it the link showed in the status bar?
Is it the actual href? If it is, you probably use some view helper which construct your "base url"
Maybe you "link" your resource without specifing an absolute path (using /), then your resource are "relatively" linked to current page (/articles/)
.htaccess don't change anything in your code.
There is an html element which allow you to define base url used everywhere in your page.
<base href="/root" />
if you have for example Article 12 then when clicking on the link, you will redirect to /root/articles/12
Also, mixing Pekka's answer with Boris', you should define somewhere in your application which is the root path of your application and output all paths as absolute, prepending the base dir you defined earlier.
for example: in config.inc.php
define("ROOT_URI", "http://myserver.com/myapp");
everywhere:
<img src="<?php echo ROOT_URI;?>/media/2011/02/21/logos.jpg
This is like using the base element as Boris suggested, without using it (I also dislike base), and makes your application able to work in whatever folder under the webserver it is stored.