.htaccess Problem
Page 1 of 2 12 LastLast
Results 1 to 15 of 21

Thread: .htaccess Problem

  1. #1
    Senior Member
    Join Date
    Apr 2003
    Posts
    167

    .htaccess Problem

    Hi all,

    I'm not at all familiar with .htaccess, so I don't know if this is the correct forum if what I am doing is anywhere near right.

    Anyway...

    I'm trying to redirect from one page to another using a simple rule. However, for whatever reason it is not working, could someone take a look?

    Code:
    RewriteRule ^directory/alabama/$ directory/state-page/
    Thanks for any help.

    Mike

  2. #2
    Senior Member
    Join Date
    Apr 2003
    Location
    Silver Lake
    Posts
    4,851
    "not working" how?

    It looks to me like any request for http://example.com/directory/alabama/ should be redirected to http://example.com/state-page/.

    A couple of things:
    * The request must be *exactly* directory/alabama and nothing else. The ^ means start of request and the $ means end of request so this would not apply to a request for directory/alabama/index.php.
    * I'm not sure if a request would start with a slash or not. You might consider trying:
    Code:
    RewriteRule ^/directory/alabama/$ /directory/state-page/
    I'm not at all sure about that.
    * Depending on your server setup, your system may just ignore .htaccess files.

    Is there other information in your .htaccess file? Are you sure your system is configured to use it?
    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

  3. #3
    Senior Member
    Join Date
    Apr 2003
    Posts
    167
    Hi,

    Thanks for your response.

    The website I'm attempting this on is based on Wordpress, meaning that the whole .htaccess file reads as follows:

    Code:
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    
    RewriteEngine On
    RewriteRule ^directory/alabama/$ directory/state-page/
    When I say it doesn't work I mean it simply doesn't redirect to the specified page :S

    It's worth mentioning that the "directory" page is a page made through wordpress, so doesn't exist as a hard file on the system so to speak. I'm not sure if this makes a difference?

    Could anyone help?

    Many thanks,
    Mike

  4. #4
    Senior Member
    Join Date
    Apr 2003
    Location
    Silver Lake
    Posts
    4,851
    Does redirection work on your system? It's important to determine that before we fix it some other way.

    You don't need two RewriteEngine directives. take out the second one.

    Put your rule inside the <IfModule> brackets right before the last RewriteRule.

    The [L] means "if this rule applies then it is the last one and you should stop parsing". I think that's your problem because redirections never reaches your redirect at the end.
    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

  5. #5
    Senior Member
    Join Date
    Apr 2003
    Posts
    167
    Thanks for your reply.

    My file now looks like the following, but still nothing happens. I'm assuming redirects are set up on my server and the problem could be wordpress related?

    Code:
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^directory/alabama/$ directory/state-page/
    RewriteRule . /index.php [L]
    </IfModule>
    Thanks,
    Mike

  6. #6
    Senior Member
    Join Date
    Apr 2003
    Location
    Silver Lake
    Posts
    4,851
    I've asked a couple of times now -- do you know that your server will pay any attention to an .htaccess file? Before we try tweaking this, it would be good to know if your server is configured to allow .htaccess redirection. It may not have mod_rewrite.c installed (this is apache module that handles redirects and the reason for those <IfModule> directives) or it may not be configured to allow rewrites in an .htaccess file.
    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

  7. #7
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,414
    Put your 'alabama' rule above the two RewriteCond's (conditions that were meant to be applied to the '.' RewriteRule but are now being applied to the 'alabama' rule instead). In addition, you'll want to add a [L] flag to it to denote that it is the last rule in the chain that Apache should match against before actually doing the rewrite. Otherwise, Apache will continue checking subsequent RewriteRule's to see if it can find another match (which it can - the last rule).

  8. #8
    Senior Member
    Join Date
    Apr 2003
    Location
    Silver Lake
    Posts
    4,851
    BG is right, your special rule will have to go before the RewriteCond lines, but "it doesn't work" might mean that your server doesn't give a hoot about your htaccess file.
    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

  9. #9
    Senior Member
    Join Date
    Apr 2003
    Posts
    167
    Thanks for your responses. I have just checked and yes, the server does pay attention to the .htaccess file. I also tried putting the rule in the position that bradgrafelman said and this seemed to work as well.

    However, I think I need the rule to come at the end (after the other rule has been processed). This is because I am redirecting to a page formed by that next rule. Is there any method to accomplish this?

    Thanks for the help so far.

  10. #10
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,414
    Quote Originally Posted by webmasternovis View Post
    This is because I am redirecting to a page formed by that next rule.
    Oh, so you do want that last rule to take effect. Well, in that case you'd want to remove the [L] flag so that Apache continues processing the rest of the RewriteRules after it matches your rule.

  11. #11
    Senior Member
    Join Date
    Apr 2003
    Location
    Silver Lake
    Posts
    4,851
    Quote Originally Posted by webmasternovis View Post
    Thanks for your responses. I have just checked and yes, the server does pay attention to the .htaccess file. I also tried putting the rule in the position that bradgrafelman said and this seemed to work as well.
    Great!

    Quote Originally Posted by webmasternovis View Post
    However, I think I need the rule to come at the end (after the other rule has been processed). This is because I am redirecting to a page formed by that next rule. Is there any method to accomplish this?
    Obviously, it doesn't work at the end. You've tried that. The reason why is the [L] flags which say "do not continue past this point".

    Let's take a look at the lines in your rules one by one:
    Code:
    <IfModule mod_rewrite.c> -- only use the following section if mod_rewrite is installed!
    RewriteEngine On -- turn the rewriting functionality ON
    RewriteBase / -- explicitly sets the base URL for per-directory rewrites
    RewriteRule ^index\.php$ - [L] -- if the request is actually for index.php, then don't rewrite anything and THIS IS THE LAST RULE.
    RewriteRule ^directory/alabama/$ directory/state-page/ -- your rewrite rule which just says replace the old request for your new one and *keep checking rules*
    RewriteCond %{REQUEST_FILENAME} !-f -- only apply the next RewriteRule if the request is not for an actual file
    RewriteCond %{REQUEST_FILENAME} !-d -- only apply the next rewrite rule if the request is not for an actual directory
    RewriteRule . /index.php [L] -- any request at all that reaches this point will be routed to index.php and THIS IS THE LAST RULE
    </IfModule>
    Obviously, nothing can get past that last rule. Also, there' s no harm in putting your rewrite rule before the other rules because it will only affect that one request and will do so before the special processing gets done.
    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

  12. #12
    Senior Member
    Join Date
    Apr 2003
    Posts
    167
    Hi,

    Thanks for your responses again, I really do appreciate them. At the moment, I have the following code:

    Code:
    <IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteRule ^directory/alabama/$ http://www.mysite.com/?p=21
    RewriteCond &#37;{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
    </IfModule>
    Now, this works perfectly - apart from one thing. I don't want the user to know they have been redirected. Do you know how I could achieve this?

    Again, thanks for all the help.

    Mike

  13. #13
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,414
    If you don't want the user to know they've been redirected, then don't do an external redirection. By supplying a full URI in the RewriteRule, you're telling Apache that you want to send an HTTP 301 status and a 'Location' header containing the new URL. If the destination URL is on your own server, though, this is highly unnecessary since the user is already at the destination server.

    Instead, simply rewrite the portion of the URL after the protocol and domain name (e.g. don't use a full URI).

    EDIT: See this part of the Apache manual page for mod_rewrite for more information on the types of substitutions you can do.

  14. #14
    Senior Member
    Join Date
    Apr 2003
    Location
    Silver Lake
    Posts
    4,851
    You cannot redirect from one domain to another without the user knowing.

    If all of this is done for mysite.com, then instead of using a fully qualified domain, you could something like this:

    Code:
    RewriteRule ^directory/alabama/$ /?p=21
    EDIT: or perhaps this:
    Code:
    RewriteRule ^directory/alabama/$ /index.php?p=21
    IMPORTANT: STOP using the mysql extension. Use mysqli or pdo instead.
    World War One happened 100 years ago. Visit Old Grey Horror for the agony and irony.

  15. #15
    Senior Member
    Join Date
    Apr 2003
    Posts
    167
    Thanks for your reply.

    I've tried your suggestion, yet for some reason it still shows the new URL. This seems strange as if I do a standard html page (eg test.html), the URL doesn't change. Do you know why this could happen?

    Thanks again for your help.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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