I have the following PHP code:
<?php
if(isset($_GET['article']))
{
echo "<b>success</b>";
}
?>
And this html:
<a href="http://localhost/PHPTest/index.php?article_27" >Click me</a>
I am trying to come up with my own url.
By doing so, I try to use mod_rewrite(). This is what is in my .htaccess:
RewriteEngine On
RewriteBase /PHPTest/
RewriteRule ^article_([0-9]+)$ index.php?article=$1
As you can see, I try to get the article get variable..
This however doesnt work.. The htaccess does not comprehend artcile_27, for example, as article=27..
Why is that?
What should happen is that the success word should be printed whenever I press the link. The problem is that it does not.
The rule you have written would match the link http://localhost/PHPTest/article_27, if you really want to have the URL http://localhost/PHPTest/index.php?article_27 matched (which is an, at least in my oppionion, unusual URL), the correct Rewrite Rule would be the following:
RewriteRule ^index\.php\?article_([0-9]+)$ index.php?article=$1
You have to look at the whole part after the last slash.
Your rule should be:
RewriteRule ^index.php?article_([0-9]+)$ index.php?article=$1
However, I'd question the use of mod_rewrite in this way.
First of all, if your url is always just index.php?article_27 then you can get directly at "article_27" using $_SERVER['QUERY_STRING'], without mod_rewrite at all.
If you're going to use mod_rewrite, why not have your URL be /PHPTest/article/27?
RewriteRule ^article/([0-9]+) index.php?article=$1
The above will also allow /PHPTest/article/27/title_of_article_here, which gives you two nice things: the ID is still there so it's easy to retrieve server-side, and the name is in the URL so it's nice to read as a human and helps search engines. If you look in your URL bar now, you'll notice stackoverflow itself uses this method.
Open this: http://localhost/PHPTest/article_27
If it works, it means that your rewriting works. In that case, all you have to do is change the HTML:
<a href="http://localhost/PHPTest/index.php?article_27" >Click me</a>
Should be:
<a href="http://localhost/PHPTest/article_27" >Click me</a>
Or rather, just:
<a href="/PHPTest/article_27" >Click me</a>
Which makes your code more portable to another hostname, port, protocol ...
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'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?
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' :)
hello i have an anchor on my site. by calling my site the url will be:
http://site.com/page.php
by clicking a button the url will display an url that i would like to hide:
http://site.com/page.php#1
now i have found out that i can change this by using mod_rewrite tool. i tried this rule without success:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^(.*)\.html$
RewriteRule ^.*\.html$ %1.php [L]
for calling the page as html or php and to hide the #1 behind the .php
RewriteRule ^(.*)$ ./php#1=php$1
i really would appreciate if there is someone who could give me advise on how to solve this. thanks a lot.
You cannot achieve this via mod_rewrite.
URL after # are not passed through server.
If you still want to achieve this, you may want to look into doing it via JavaScript:
Check this out
JavaScript:
function scrollHere() {
// Scroll to the anchor
//(you may need to look for some script to achieve this)
return false; // to prevent URL overwriting
}
One such script is this jQuery.scrollTo.
The hashtag is never sent to the server, thus you simply cannot create a rule, that based on it.
You can do something like this to remove # tag.
Your text.
So when you write "javascript:void(0);" in href attribute of a tag and thats it.
HTML
<a href='page'>Home Page</a>
<a href='page/1'>Page 1</a>
<a href='page/2'>Page 2</a>
<a href='page/3'>Page 3</a>
Rewriting as
RewriteRule ^$ page.php [L]
RewriteRule ^([a-z]+)/?([0-9]*)/?$ page.php?index=$2 [L]
And check for index in php
<?php
if (isset($_GET['index']))
{
$pageNo =$_GET['index'];
}
//based on page NO display contents
?>
I have a html file that has an image:
<img src="../smilies/happy.gif" />
Is it possible to redirect this path to another using .htaccess?
So the above example would become equivalent to
<img src="http://static.example.com/smilies/happy.gif" />
Well, you might get away with redirecting all calls that end with /smilies/happy.gif to that directory, so it wouldn't matter where the call came from. That does mean you should not wish to call this to some other subedirectory, but I imagine you don't.
something like this (guessing here, cann't test, so read up on the rwriting there :)
RewriteRule ^(.*)/images/$(.*) http://static.example.com/smilies/$2 [L,R=301]
basically you are rewriting everything that has '/images/' in it to that static adress, pasting whatever was after images after the new asdress (the $2 thingy) and then indicating that this is the last command to parse (to stop strange things in the htaccess) and that you want a 301 (permanently moved) code to be sent.
I think its easier to set a variable in your config
<?php
$static_url = "http://static.example.com/";
?>
<img src="<?php echo $static_url; ?>/smilies/happy.gif" alt="" />
Its better then you have the control what came from static and the rewrite engine is slow when you call it everytime its not sooo good.
RewriteRule ^(smilies/.*)$ http://static.example.com/$1 [R]