CodeIgniter Forums
htaccess 301 redirect help please - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: htaccess 301 redirect help please (/showthread.php?tid=19572)

Pages: 1 2


htaccess 301 redirect help please - El Forum - 06-11-2009

[eluser]rt30000[/eluser]
Hello. I have created a new site and want to use a 301 redirect for old pages to the corresponding page on the new site. I can get it working, but it appends the location to the query string. Here is an example...
http://lakesideohio.com/LakesideExperience/Directions.aspx redirects correctly to http://lakesideohio.com/directions...but you will notice it adds the "?/LakesideExperience/Directions.aspx" to the URL. Here is my htaccess file if it helps. Thanks for any help you can provide, I am new to htaccess files.

Code:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    
    # 301 Redirects (old site pages redirected to new ones)
    redirect 301 "/LakesideExperience/Directions.aspx" http://lakesideohio.com/directions
    
    
    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
    
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

    ErrorDocument 404 /index.php
</IfModule>



htaccess 301 redirect help please - El Forum - 06-12-2009

[eluser]rt30000[/eluser]
Still haven't had any luck, anyone have a clue?


htaccess 301 redirect help please - El Forum - 06-12-2009

[eluser]TheFuzzy0ne[/eluser]
Sadly, there doesn't appear to be a resident htaccess expert within the community. You might be better off asking in a forum where there are such experts.


htaccess 301 redirect help please - El Forum - 06-12-2009

[eluser]Dam1an[/eluser]
[quote author="TheFuzzy0ne" date="1244832103"]Sadly, there doesn't appear to be a resident htaccess expert within the community. You might be better off asking in a forum where there are such experts.[/quote]

Yes there is, there's Daniel... somewhere, although I've not seen him around (metaphorically speaking) for a while


htaccess 301 redirect help please - El Forum - 06-12-2009

[eluser]rt30000[/eluser]
I dropped him a quick line from his contact form pointing to this thread. Hopefully he has a minute to take a look. Thanks guys. I really need to learn more about regular expressions and htaccess rules, I always get stumped by them.


htaccess 301 redirect help please - El Forum - 06-12-2009

[eluser]Daniel Moore[/eluser]
I have been rather busy lately, and just haven't had the opportunity to keep up with the forums here. Got the message, though. Good idea to contact me like that when you need help.

For starters, let me recommend you make sure that your "mod_alias" module is installed/enabled in apache. The redirect command is not a part of mod_rewrite, and you have it under that section as follows:
Code:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    
    # 301 Redirects (old site pages redirected to new ones)
    redirect 301 "/LakesideExperience/Directions.aspx" http://lakesideohio.com/directions

If should be as follows:
Code:
<IfModule mod_alias.c>
   redirect 301 "/LakesideExperience/Directions.aspx" http://lakesideohio.com/directions
</IfModule>
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

...continue the rest of your .htaccess here...

You may want to verify that you have mod_alias enabled. To do so, set up an .htaccess as follows:

Code:
<IfModule !mod_alias.c>
   ASDF! THROW500ERROR!
</IfModule>

If you get a 500 Internal Server Error with that, then you do not have access to mod_alias, and therefore cannot redirect in that manner.

Now, about this line of code:
Code:
redirect 301 "/LakesideExperience/Directions.aspx" http://lakesideohio.com/directions

That code is correct, assuming the old address is at http://lakesideohio.com/LikesideExperience/Directions.aspx. Your most likely problem is not having mod_alias enabled.

If mod_alias is enabled and you are still having that problem, then it is possible that the AllowOverride does not include the "FileInfo" for override.

If you are unable to enable mod_alias or do the override, then you can still use the mod_rewrite module and do a rewrite instead of a redirect. It will do the same thing if you specify a 301 redirect using the appropriate flag [R]. It can be done as follows:

Code:
RewriteRule ^/somepath(.*) /otherpath$1 [R=301]

OR for an absolute redirect:

Code:
RewriteRule ^/somepath /otherpath [R=301]

Hope this helps. Let me know if it doesn't. I've subscribed to this thread, so a reply will now alert me by email.


htaccess 301 redirect help please - El Forum - 06-12-2009

[eluser]wiredesignz[/eluser]
[quote author="TheFuzzy0ne" date="1244832103"]Sadly, there doesn't appear to be a resident htaccess expert within the community...[/quote]
I would defer to Daniel Moore here, as he has provided us with the best resource for managing htaccess in CI applications. http://www.danielwmoore.com/remove_index_from_codeigniter

EDIT:
OOP's see post above Tongue


htaccess 301 redirect help please - El Forum - 06-12-2009

[eluser]TheFuzzy0ne[/eluser]
Excellent resource! Thanks for the link, and thanks Daniel for creating it.


htaccess 301 redirect help please - El Forum - 06-12-2009

[eluser]rt30000[/eluser]
Daniel, you rock! Thanks for the guidance, I will hopefully get a chance to test this later today. Thanks, I'll post back my results.


htaccess 301 redirect help please - El Forum - 06-12-2009

[eluser]rt30000[/eluser]
I have revised my htaccess file as suggested but still get the same result. I must have to do it using mod_rewrite then? I have a TON of these urls to redirect. Here is the revised htaccess file...
Code:
<IfModule !mod_alias.c>
    #If we get a 500 Internal Server Error with this then we do not have access to mod_alias, and therefore cannot redirect in this manner
    ASDF! THROW500ERROR!
</IfModule>

<IfModule mod_alias.c>
    # 301 Redirects (old site pages redirected to new ones)
    redirect 301 "/LakesideExperience/Directions.aspx" http://lakesideohio.com/directions
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    
    #Removes access to the system folder by users.
    #Additionally this will allow you to create a System.php controller,
    #previously this would not have been possible.
    #'system' can be replaced if you have renamed your system folder.
    RewriteCond %{REQUEST_URI} ^system.*
    RewriteRule ^(.*)$ /index.php?/$1 [L]

    #Checks to see if the user is attempting to access a valid file,
    #such as an image or css document, if this isn't true it sends the
    #request to index.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?/$1 [L]
    
</IfModule>

<IfModule !mod_rewrite.c>
    # If we don't have mod_rewrite installed, all 404's
    # can be sent to index.php, and everything works as normal.
    # Submitted by: ElliotHaughin

    ErrorDocument 404 /index.php
</IfModule>

and you will note http://lakesideohio.com/LakesideExperience/Directions.aspx still results in the URL http://lakesideohio.com/directions?/LakesideExperience/Directions.aspx