Log in

View Full Version : About Htaccess Redirect



round
01-11-2013, 02:17 PM
Can any one please tell me how to redirect -

http://gmoz.geliyoo.com/index.php?c=373313&computer (there will be dynamic number after ?c=)

To

http://gmoz.geliyoo.com/computer

I did following but it only removes index.php

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ /%1 [R=301,L]

Thanks
Kaushal

traq
01-11-2013, 02:59 PM
You're thinking about this backwards:

You don't redirect index.php?do=whatever to my/pretty/url.

If you want to change your URLs into "friendly" URLs, just change them. what .htaccess does is redirect them to the matching "messy" url, so your website understands what page to show. That way, your user will write (or click) my/pretty/url, and your website will see index.php?do=whatever.

In your case, you might want to redirect /computer/42 to index.php?computer=42. Does that sound right?

round
01-12-2013, 04:19 AM
Dear Traq, Thank you for your kind reply :) but what I want is to redirect

http://gmoz.geliyoo.com/index.php?c=373313&computer (there will be dynamic number after ?c= and also any string after &)

To

http://gmoz.geliyoo.com/computer

Yes you right that basically I need user friendly urls. so I am having issue that currently I am having http://gmoz.geliyoo.com/index.php?c=373313 type of urls whenever user clicks on any category. so what I thought.....to put category name after ?c=373313 like ?c=373313&computer so when user hit.......http://gmoz.geliyoo.com/index.php?c=373313&computer then to remove "index.php?c=373313&" from url so that becomes like http://gmoz.geliyoo.com/computer.

And yes now it will be very tough for me to change ?c=... to friendly like computer because I use $_GET['c'] in many places. :(

Any Idea Traq?

Thanks
Kaushal

traq
01-12-2013, 05:10 AM
...what I want is to redirect
http://gmoz.geliyoo.com/index.php?c=373313&computer (there will be dynamic number after ?c= and also any string after &)
To
http://gmoz.geliyoo.com/computer

Yes you right that basically I need user friendly urls.
Okay... I'm pretty sure I understand what you mean. Let me rephrase my earlier answer:

You don't redirect /index.php?c=373313&computer to /computer.

You do redirect /computer to /index.php?c=373313&computer.
This means that your user will type/click on /computer (happy user), and your website will see /index.php?c=373313&computer (happy website).


... so when user hit.......http://gmoz.geliyoo.com/index.php?c=373313&computer then to remove "index.php?c=373313&" from url so that becomes like http://gmoz.geliyoo.com/computer.
That's the "backwards" part.

You don't want your user to have to type/click /index.php?c=373313&computer, because that's not "friendly." You want them to be able to just type /computer, and arrive at the correct page.

You make this change by simply deleting <a href="/index.php?c=373313&computer">my link</a> and writing <a href="/computer/373313">my link</a> instead.

When the user clicks that link (or types that URL), you have mod_rewrite set up to redirect it to the actual page that your website expects: at your original, "messy" URL.

Example:

# in your .htaccess file...
RewriteEngine On

RewriteRule computer/?([0-9]*) /index.php?computer&c=$1
Explanation:

Directive:
RewriteEngine On..............turns on mod_rewrite
Directive:
RewriteRule...................starts a mod_rewrite rule
Pattern to match:
computer......................matches the word "computer" in the URL
/?............................matches an optional "/" after "computer"
..............................(this is so "computer" by itself will still be matched, even if they forget the trailing "/")
([0-9]*)......................matches a string of numbers.
..............................the parenthesis ( ) create a backreference to this match (see below)
Where to redirect:
/index.php?computer&c=$1......rewrites the URL to the "messy" form that your website understands.
..............................we're now calling the index.php page, instead of the (nonexistent) computer page.
..............................also note that we wrote a query string, so your code that uses the $_GET superglobal will still work:
..............................we add 'computer', as well as 'c'. $1 is a backreference: it inserts the numeric string we matched earlier here.

In the end, your user can type http://www.yourdomain.com/computer/373313, and will be transparently redirected to the "real" page at /index.php?computer&c=373313. The user never sees the "messy" URL; your website never sees the "friendly" URL. Everyone's happy.

round
01-12-2013, 06:46 AM
Thank you Traq for your kind reply :)

Now I am trying that & if any problem arises then will again tell you ok? :)

Thank you again Traq.

Thanks
Kaushal

traq
01-12-2013, 03:10 PM
no prob

If your question has been answered, please mark your thread "resolved":
On your original post (post #1), click [edit], then click [go advanced]. In the "thread prefix" box, select "Resolved". Click [save changes].

round
01-18-2013, 07:34 AM
Dear Traq,

I tried your given things & my .htaccess is currently having the following things in it...
DirectoryIndex index.php
Options +FollowSymLinks
RewriteEngine on
RewriteRule Computers/?([0-9]*) /index.php?Computers&c=$1

I have taken "Computers" because I am having that category so for example I took that............ok?

In my php file I have given href like you said that is <a href="/Computers/373313">computer</a>
When user click on it.....I am handling it like $_GET['c'] in my index.php & I also checked the $_GET['c']. It is having the value 373313 but.....................

Now having following problems :-
1) It is not redirecting properly some thing wired looking page comes. Please check by hitting...............gmoz.geliyoo.com/Computers/373313
2) Another thing is...........In place of computer I can have any category. You can check that in............gmoz.geliyoo.com/index.php?list=latest (UNDER MAIN CATEGORIES)............so what should I write in place of Computers? (It is occurring 2 times in that statement in .htaccess)

Thanks
Kaushal

traq
01-18-2013, 03:08 PM
Well, I don't know what your page is supposed to look like, so I can't say for sure. The only thing I see is that your images are using relative URLs, so instead of looking for them in /images/whatever.jpg, the browser is looking for them in /computers/373313/images/whatever.png.

You need to use absolute or root-relative URLs instead (I prefer root-relative):

/images/whatever.jpg

As for the other categories, there's two ways you might approach that:

1) if there are only a few categories, just add new RewriteRules for each one.

2) if there are a lot, or if they might change, match a pattern instead:
# in your .htaccess file...
RewriteEngine On

# these lines must come first - with this approach, you'll redirect a lot of legit URLs unless you're careful.
# these conditions allow redirects *only* for URLs that do not point at real files/folders:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

RewriteRule ([a-zA-Z]+)/?([0-9]*) /index.php?$1&c=$2
# [a-zA-Z]+ matches one or more letters before the first forward slash.
# This is now $1 ([0-9]* is now $2).
# note that if your categories need to include numbers, dashes, underscores, etc., you'll need to change this group.

round
01-19-2013, 05:03 AM
Dear Traq,

Thank you very very much. It is working nicely means we can get the category number in $_GET['c] in index.php. :)

But the final thing is......URL remains same like gmoz.geliyoo.com/Computers/373313. It should be gmoz.geliyoo.com/Computers.
Means no number.

Can you tell me how it should be done?

Thanks
Kaushal

traq
01-19-2013, 05:46 AM
Simply leave it off...? However, without the number, $_GET['c'] will be empty.

I think you may still be misunderstanding what mod_rewrite does.

It's not about changing what the user types/sees in the address bar;
it's about typing/linking to whatever-you-like in the address bar and rewriting it on your server so it goes to the right page.
Whatever "friendly URL" you use still needs to include the information necessary to do that.

There's no way for the server to guess/infer the number you want for $_GET['c'] - it must be passed in the URL somehow.

round
01-19-2013, 05:53 AM
Dear Traq,

Thanks for your answer :)

So it won't be possible to remove number?

and one more thing is I am heaving another problem too...like I am also having category with spaces.....for example "computer peripherals" (It is not working. I mean with space)
and another thing is I am also having category with multiple language then? (It is not working. I mean if language is other than English)

Thanks
Kaushal

traq
01-19-2013, 06:20 AM
So it won't be possible to remove number?
As I said, you can leave the number off if you want to. But then, you won't know what it is.


...for example "computer peripherals" (It is not working. I mean with space)
In general, it's a good idea to stay away from spaces in URLs. Different browsers do different things with spaces - e.g., some urlencode them ( %20 ), while others replace them with a plus character ( + ). If you want word separators, common substitutes are underscores ( _ ), pluses ( + ), or dashes ( - ), with dashes being the current trend. Dashes make the most sense to me, and I've heard the claim that they're best for seo as well.


and another thing is I am also having category with multiple language then? (It is not working. I mean if language is other than English)
Are you using non-latin characters (i.e., anything outside the English alphabet)?

You'd need to change the first pattern (which currently matches only English letters) to match the other letters as well. I think the simplest way to do this would be to match *anything* - except a forward slash, otherwise it'd match the entire URL no matter what.

However, there's another problem: non-English* characters are urlencoded when sent by the browser, and then decoded by mod_rewrite to apply your rewrite rules, etc.. Problem is, if you use any backreferences ($1, $2, etc.) they aren't re-encoded before being sent on their way to the new URL, so the new URL is broken. You can fix this by adding the [B] flag at the end of your rule:
RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

## UNTESTED. ##
RewriteRule ([^/]+)/?([0-9]*) /index.php?category=$1&c=$2 [B]
*over-generalization.

After that, I'd recommend sorting it out in PHP: basically, instead of
<?php
if( $_GET['category'] === 'computers' ){ /* computer-stuff */ }
You'd make a map that includes the category name in whatever languages you plan to support; something like:
<?php

$language_map = array(
'computers' => array(
'computers'
,'tietokone'
,'arvuti'
,'datamaskin'
,'komputador'
)
,'othercategory' => array( 'etc...' )
);

if( in_array( $_GET['category'],$language_map['computers'] ) ){ /* computer-stuff */ }

**********
beyond that...


We're moving into the territory of things I haven't tried before. You'll need to dig in and do some research.

things I turned up in a quick google search (http://lmgtfy.com/?q=mod_rewrite+unicode):
http://dracos.co.uk/code/apache-rewrite-problem/
http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_b

round
01-19-2013, 07:53 AM
Dear Traq,

Thank you for your answer. I really appreciate your stuff :)

humm............I am very tired because client wants urls without number :)

Whatever categories we are having those may be duplicated. so we can even not search that......directly in DB & get it's ID.

Any ways I am looking for it. (Actually I searched many many many times this things but could not succeed so that I put question in this form Traq) :)

Traq thanks again :) for helping me much. If I need your help further I will again post in this issue only is that ok Traq?

Thanks
Kaushal

traq
01-19-2013, 03:15 PM
Of course, no problem.

As a developer, part of your job is to explain to your client how the web works - if they want the information that that number describes, then they need to allow the number in the URL. The website can't "read the user's mind" or "guess" what that number should be.

Is there any way you could represent that number textually ...?
What does 373313 mean? If it's an id for a model or particular part, maybe you could use that description instead (and map it to the correct number in your application, as I suggested with the "language map" above (BTW, you can still do that if all the categories are stored in the DB: might be easier, in fact)?

e.g., example.com/computers/lenovo-thinkpad-R-61 ?

round
01-21-2013, 04:31 AM
Dear Traq,

Yes we are now doing like that way. As you suggested that to use category name :)
It will slow down speed but will work as client says :)

Thank you Traq for your all replies. Thanks again :)
It is my pleasure that you assist me deeply each & every time. :)

Thanks
Kaushal

traq
01-21-2013, 06:06 AM
I'm glad it's working out. Good luck!