Page 1 of 2 12 LastLast
Results 1 to 10 of 16

Thread: About Htaccess Redirect

  1. #1
    Join Date
    Apr 2012
    Posts
    85
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default About Htaccess Redirect

    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
    Last edited by round; 01-11-2013 at 02:52 PM. Reason: to be a bit specific

  2. #2
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    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?

  3. #3
    Join Date
    Apr 2012
    Posts
    85
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default

    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

  4. #4
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    Quote Originally Posted by round View Post
    ...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).

    Quote Originally Posted by round View Post
    ... 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:
    Code:
    # 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.
    Last edited by traq; 01-12-2013 at 05:16 AM.

  5. #5
    Join Date
    Apr 2012
    Posts
    85
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default

    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

  6. #6
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    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].

  7. #7
    Join Date
    Apr 2012
    Posts
    85
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default

    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
    Last edited by round; 01-18-2013 at 11:25 AM. Reason: some change....so read it carefully

  8. #8
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    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:
    Code:
    # 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.

  9. #9
    Join Date
    Apr 2012
    Posts
    85
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Default

    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

  10. #10
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 516 Times in 502 Posts
    Blog Entries
    5

    Default

    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.

Similar Threads

  1. Resolved .htaccess redirect
    By james438 in forum PHP
    Replies: 7
    Last Post: 01-07-2012, 03:14 PM
  2. .htaccess 301 redirect
    By chetanmadaan in forum Other
    Replies: 4
    Last Post: 08-06-2010, 04:03 AM
  3. .htaccess Redirect
    By tomyknoker in forum Other
    Replies: 4
    Last Post: 04-29-2008, 12:48 AM
  4. redirect using .htaccess and ModRewrite
    By tomyknoker in forum Other
    Replies: 4
    Last Post: 10-19-2006, 04:06 PM
  5. Replies: 3
    Last Post: 12-18-2005, 04:48 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •