htaccess working mechanism - php

I have the folowing htaccess file:
RewriteEngine on
RewriteRule ^(.*)-(.*)-(.*)$ index.php?page=$1&id=$2&im=$3
And inside the php page I have:
href="home-46-126" //Contact Us
href="home-38-129" //News
I want to know how the redirection done?
What is the function that did this redirection ?
How Does the website know where to go if the user clicks on "Contact Us" ?

It isn't htaccess function, htaccess just parse "home-46-126", and redirect to index.php with these values.
If you want to work later with these values, you have to type it in PHP script.
<?php
$_GET["page"]; // including "home"
$_GET["id"]; // including "46"
$_GET["im"]; // including "126"
?>
Then you have to put these values to database or somethink, where you find out, that this ID and IM is page with Contact Us

Related

How to make dynamic pages seo friendly url using htaccess

In my php page i have given link as href="test.php?category="xyz".
In url I want this page link to appear as "test/xyz".
In my .htaccess file i have added
RewriteRule ^test/([a-zA-Z]+) test.php?category=$
but this does'nt work for me.
All the other links in test.php gets test appended to their links.
eg. if there was example.php its appears as test/example.php
Help is appreciated.
Thanks in advance
Maybe this code can help you
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^test/([a-zA-Z]+)$ test.php?category=$1
</IfModule>
Try to access these urls : test.php?category=abc and test/abc. If two pages show the same content this code successful.
To learn more, please read the reference here
This sounds more like you forgot the use full relative paths in your links. Instead of using:
example
You should use:
example
Else when you are on page /test/abcd, going to example.php means /test/example.php

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' :)

Block direct access to PHP files

My present htaccess file contains mod_rewrite rules to perform this:
www.mysite.com/articles/jkr/harrypotter --> www.mysite.com/index.php?p=articles&author=jkr&book=harrypotter
www.mysite.com/articles/jkr --> www.mysite.com/index.php?p=articles&author=jkr
www.mysite.com/articles --> www.mysite.com/index.php?p=articles
In my root directory, I have some PHP files like: index.php, pre.php, view.php, etc.
What I want: block direct access to php files.
Eg:
www.mysite.com/index.php --> "404 - Page Not Found" or "File not exist"
www.mysite.com/view.php --> "404 - Page Not Found" or "File not exist"
I have tried a code that I found on searching. It uses "f" flag. But i did not understand it. When I used it do not show the home page also. So, I removed everything that I was testing. My htaccess file now contains three rules only(to accept page, author, book).
Please help.
I saw another question that was previous asked in here : mod_rewrite error 404 if .php
A change that I made to it is, added [NC] flag. Otherwise, if we put the uppercase letter in extension(say, "index.PHP") it will load.
So,
SOLUTION:
RewriteCond %{THE_REQUEST} \.php[\ /?].*HTTP/ [NC]
RewriteRule ^.*$ - [R=404,L]
thank you
Create a simple 404.php that throws a 404 error, then rewrite index.php to 404.php etc.
You can also do this at your script level. If you add a rule within your index.php to check whether you have been passed GET variables or just called the page without any parameters like so:
<?
if(empty($_GET))
header('Location: http://www.yoursite.com/404.php');
else
//your usual script
?>
Then you have control to either redirect people to a 404 error or show something more helpful, like a landing page with an index or some form of website navigation.

Can I hide a portion of my URL?

I asked a simialr question earlier about using jquery and hashtags but what I really need to do is after a successful login redirect the user to the home page (home.php) Ideally I want to load my header with my menu only once and then update my main div tag with the contents of home. This will let me load the menu bar just once and not after every page load. I'm currently calling the home page like so:
if(isset($success) {
header('Location: menu.php?home');
exit();
}
Is it possible to hide the menu.php portion of the URL to the end user or is there a better way?
Thanks
Maybe look at apaches mod_rewrite. Tutorial
Is it possible to hide the menu.php portion of the URL to the end user?
No.
is there a better way?
You can use a mod_rewrite rule to rewrite /home to /menu.php?home.
what about renaming menu.php to index.php & using it like
if(isset($success)) {
header('Location: /?home');
exit();
}

after .htaccess url rewrite, cannot perform logoff in some of the url rewrited page

Recently, I was doing .htaccess url rewrite, make all my php url into html, in some page, the logout button wont work properly. for example, in page ‘quotedetails/Q9999.html’ (rewrited from ‘quotedetails.php?quoteID=Q9999′), when I click logout button in this page, it wont do the trick, but when i use the old php url of this page, it works again, other rewrited pages like index.html (index.php), search.html(search.php), all works perfectly.
I use firebug to debug, after I click the logout button, it stays in the same page without redirect me to the index.html, but I saw the the ‘logoff’ params has been passed through, but just dont let me logout and redirect to index page. I’ve changed all the relavent file path to absolute path, still no luck…..help please.
I’ve also noticed from firebug, that page cannot get the redirect ‘location’ as I tried in other pages, their response headers come with ‘location: index.html’, but in that no-workin-page, there is no such line called ‘location: index.html’ in its response headers.
Here is my .htaccess file, no-workin-pages are related to the first four ReweiteRules
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^reps/all,all.html$ rep.php?repID=all&repName=all
RewriteRule ^reps/([A-Z]+),([A-Za-z\sA-Za-z]+).html$ rep.php?repID=$1&repName=$2
RewriteRule ^reps/([A-Za-z]+),([A-Za-z\sA-Za-z]+),([0-9]+).html$ rep.php?repID=$1repName=$2&page=$3
RewriteRule ^quotedetails/(Q[0-9]+).html$ quotedetails.php?quoteID=$1
RewriteRule ^index.html$ index.php
RewriteRule ^addquote.html$ addquote.php
RewriteRule ^search.html$ search.php
RewriteRule ^viewall.html$ viewall.php
RewriteRule ^howto.html$ howto.php
all the CSS will be lost, how to fix this issue?
Use absolute path for all the CSS files and images
I click log out button, its not working
You have to do at least initial debug. Nobody here knows, what's going on when you press a button. Go figure.
You don't have to use absolute paths... most people just forget about one of the most important html-tags. write this into your -section of your html-output:
<base href="http://mysite.com" />
Now all your css-files and images should be loaded correctly.

Categories