I've used mod_rewrite to rewrite links on my site like so:
mysite.com/store/<store_id>/<store_name>/<page>
mysite.com/store.php?id=<store_id>&page=<page>
for example:
mysite.com/store/1313/johnny-walker-inc/13
mysite.com/store.php?id=1313&page=13
However, as a result, all my links that we're relationally placed now start at the end of the link, for example:
mysite.com/send_message.php
has become
mysite.com/store/1313/johnny-walker-inc/send_message.php
How can I fix this?
Here is my mod_rewrite code, in case I'm making a mistake with it:
RewriteRule ^store/([0-9]+)/[^/]+/([0-9]+)$ store.php?storeid=$1&page=$2 [L]
Thanks!
You need to make your links relative to the root, like so:
link
Note the slash before send_message.php.
I personally only see one solution: Just make all your links absolute. It's not directly a problem with mod_rewrite, but the browsers way of interpreting these links. From their perspective you have a directory structure and they interpret the relative position accordingly.
Other solutions include a BASE href or just rewriting all the page elements that can be referenced by the imaginary context root you're showing the client.
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 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">
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?
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/" />
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.