Get parameter variables in a permalink - php

I've a page that shows the last 10 articles of a database, and, with the $_GET['show-all'] variable, it shows all the articles.
Now, I want to use permalink. For the 'standard' page, the permalink is
/articles
for the second one, I might use this
/articles/show-all
But search engines as Google recognizes it as another page, and generated a duplicated meta-tag error. So I want to use this permalink instead
/articles/?show-all
But all my attempts didn't work. I tried this code in the .htaccess file:
RewriteRule ^articles/?(.*)$ /3/contents.php?p=articles&$1 [L]
or
RewriteRule ^articles/\?(.*)$ /3/contents.php?p=articles&$1 [L]

Take a look at the canonical tag
http://en.wikipedia.org/wiki/Canonical_link_element
to avoid "duplicate content" ;)

Try to always use a trailing slash or not.
Since /name != /name/
Therefore go for /name?param in your case.
this topic has a lot of information about it by the way:
When should I use a trailing slash in my URL?

Related

Htaccess rewrite (Cond and) Rule to rewrite these

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.

Issue with # in URL with a Wordpress theme?

I am having a severe problem and have no clue about what is going on... I will specify the general issue which is causing multiple issues on this Wordpress powered portal.
Steps to reproduce:
Visit the URL: http://gamersgeographic.com/en/ (or any post on this site)
Append #abc or #anything to the URL
The URL tries to resolve for a second and magically deletes the "#" and instead changes to /abc or /anything , which of course does not exist and gives a 404 page not found.
Even if the local anchor with #abc exists, behaviour is the same.
Now, consider the case below:
Visit http://gamersgeographic.com/monster-hunter-diary-1/
Comment link appends a #comments or #respond depending on whether a comment is there or not.
Both the anchors exist on the single post page
Still it redirects after finding them, to /comments and gives 404
Direct URL with #comments works e.g. http://gamersgeographic.com/monster-hunter-diary-1/#comments works but when I change any base URL to #comments, it redirects to 404...
I have tried several combinations with Permalinks, so it is not a problem with that. I even wrote my own Comment link generator in php with just a plain
href="#comments"
but still no luck...
If you need any further information about any function's code in theloop.php or anything please let me know.
Thanks in advance !
Regards
The contents of .htaccess are as below:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
This is not a PHP issue, it is Javascript: it is evident when you reproduce it, and you can test it by disabling Javascript and adding #comments at the end of the URL; it will work.
Now, I have done some work for you, and the culprit is a Javascript file aptly named hashchange.js. Look, for example, at this line:
function second_passed() {
if(current_page!=location.href {
get_page_by_hash(location.href);
}
setTimeout(second_passed,1000);
}
Which explains why you see it “working” for a second.
And here is the redirect:
jQuery(window).hashchange(function() {
var link = window.location.hash.replace("#", "");
get_page_by_hash(link)
});
Note that hashchange is a method for event handling available in jQuery Mobile.
<link rel="canonical" href="URL OF YOUR HOMEPAGE HERE>
add this in your header.php in <head></head> section and then try . it shouldn't be giving 404 error !
The way that page bookmarks are used is, as you know, the href="" of an anchor points to an #some-place. In order for this to happen #some-place must be the id of the element within the page you wish to go to.
For example:
http://gamersgeographic.com/monster-hunter-diary-1/#respond
should take you to the element with id="respond" in that page.
If the element with that ID doesn't exist you won't be able to travel to it, and may be the reason it results in a 404 Not Found. However, if the element does indeed exist on the page with the proper ID and it still redirects to a 404 then you may want to check your web server configuration to make sure it isn't filtering the # in some way.

How To: URL Rewrite & HTML Link Within Same Page?

I want a user to click on link of www.domain.com/how-it-works
My URL Rewrite is:
RewriteRule ^how-it-works/?$ index.php?action=about&link=howItWorks [NC]
index.php GETS "action" and "link" parameters, and then this is where I get lost...
I am currently using the following:
require( TEMPLATE_PATH . "/about.php" );
This loads up the about.php template file, which is exactly what I want, but then where do I put the "same page link" of: #HIW? #HIW is a link inside the about.php template file.
The #HIW in your url is called a fragment identifier in HTML.
Supposing you are using apache, you can go with (note the NE and R flags at the end):
RewriteRule ^how-it-works/?$ index.php?action=about#HIV [NC,NE,R]
(I took of &link=howItWorks because I suppose #HIV replaces it, if not you can put it back)
http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_ne
As far as I understood your URL Rewrite already carries that link information. In this case the HIW you mentioned above as the final loading url is index.php?action=about&link=howItWorks.
Why don't you $_GET['link'] inside the about.php and write the output from that result?
I mean, if you want to give the answer from the about.php file, you can get the link information with $_GET[] and give your desired result information from there.
Expand your rewrite to allow inner links:
RewriteRule ^how-it-works/?(#.*)?$ index.php?action=about&link=howItWorks [NC]
Please not I don't know if this reg ex works I just typed it out quickly. Please try a few if its not working properly.
It should 'just work' :)

How do I stop mod_rewrite from appending links to url?

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">

mod_rewrite taking over all the links on the page

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.

Categories