nmd2w
March 10, 2011, 9:45am
1
Hello,
Is there any way to redirect all urls like [noparse]example.com/home/post-title[/noparse] to [noparse]example.com/post-title[/noparse] ? using htaccess or in any means?
Background:
I’ve been managing a portal for my old school and it’s url is something like [noparse]example.com/home/[/noparse] . Now I’ve removed that “/home” and made it simply example.com . for this I’ve re uploaded all my files in “/home” directory to root directory of example.com .
But what my problem is, my school website is linked and referenced widely. Since current site url is like [noparse]example.com/post-title[/noparse] , old urls like [noparse]example.com/home/post-title[/noparse] will output 404 error and thus old referenced article will be unavailable.
Is there any way to redirect all urls like [noparse]example.com/home/post-title[/noparse] to [noparse]example.com/post-title[/noparse] ? using htaccess or in any means?
Any help will be appreciated.
Thanks in advance
_NM
rpkamp
March 10, 2011, 10:13am
2
Hi, and welcome to SitePoint
So, if I get this correctly you want every request that starts with /home/ to be redirected to the URL with /home/, so /home/whatever should just become /whatever , right?
If so, place this in a .htaccess file in the root directory of the website:
RewriteEngine On
RewriteRule ^home/(.*)$ $1 [L,R=301]
That is for Apache 2.x. If you’re on Apache 1.x change ^home to ^/home
nmd2w
March 10, 2011, 10:57am
3
rpkamp:
Hi, and welcome to SitePoint
So, if I get this correctly you want every request that starts with /home/ to be redirected to the URL with /home/, so /home/whatever should just become /whatever , right?
If so, place this in a .htaccess file in the root directory of the website:
RewriteEngine On
RewriteRule ^home/(.*)$ $1 [L,R=301]
That is for Apache 2.x. If you’re on Apache 1.x change ^home to ^/home
Hi thanks for you quick reply.
but when i tried this, that makes some duplication in the url
for example: when i access the url [noparse]example.com/portal/post-title[/noparse] (after adding RewriteRule ^portal/(.*)$ $1 [L,R=301]) it goes to [noparse]example.com/hsphere/local/home/user/example.com/post-title[/noparse] .
One thing hopeful is that, i could see the “/portal” part is removed from url.
my htaccess file is copied below (Im using Drupal cms for this portal)
AddHandler phpini-cgi .php .htm
Action phpini-cgi /cgi-bin/php5-custom-ini.cgi
<FilesMatch "\\.(engine|inc|info|install|module|profile|test|po|sh|.*sql|theme|tpl(\\.php)?|xtmpl|svn-base)$|^(code-style\\.pl|Entries.*|Repository|Root|Tag|Template|all-wcprops|entries|format)$">
Order allow,deny
</FilesMatch>
Options -Indexes
Options +FollowSymLinks
<Files favicon.ico>
ErrorDocument 404 "The requested file favicon.ico was not found.
</Files>
DirectoryIndex index.php
ErrorDocument 404 /index.php
# PHP 4, Apache 1.
<IfModule mod_php4.c>
php_value magic_quotes_gpc 0
php_value register_globals 0
php_value session.auto_start 0
php_value mbstring.http_input pass
php_value mbstring.http_output pass
php_value mbstring.encoding_translation 0
</IfModule>
# PHP 4, Apache 2.
<IfModule sapi_apache2.c>
php_value magic_quotes_gpc 0
php_value register_globals 0
php_value session.auto_start 0
php_value mbstring.http_input pass
php_value mbstring.http_output pass
php_value mbstring.encoding_translation 0
</IfModule>
# PHP 5, Apache 1 and 2.
<IfModule mod_php5.c>
php_value magic_quotes_gpc 0
php_value register_globals 0
php_value session.auto_start 0
php_value mbstring.http_input pass
php_value mbstring.http_output pass
php_value mbstring.encoding_translation 0
</IfModule>
<IfModule mod_expires.c>
ExpiresActive On
ExpiresDefault A1209600
ExpiresByType text/html A1
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^portal/(.*)$ $1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>
# $Id: .htaccess,v 1.90.2.3 2008/12/10 20:04:08 goba Exp $
any suggestions?
thanks again
_NM
rpkamp
March 10, 2011, 10:59am
4
Replace $1 with /$1
And clean your browser’s cache before you try again; browsers cache 301 redirects …
PS. If that doesn’t work put the complete URL in the rule:
RewriteEngine On
RewriteRule ^home/(.*)$ http://www.example.com/$1 [L,R=301]
That will definitely work
nmd2w
March 10, 2011, 11:22am
5
rpkamp:
Replace $1 with /$1
And clean your browser’s cache before you try again; browsers cache 301 redirects …
PS. If that doesn’t work put the complete URL in the rule:
RewriteEngine On
RewriteRule ^home/(.*)$ http://www.example.com/$1 [L,R=301]
That will definitely work
Yeah! wonderfully it’s working! I thought I was far away from the solution. thanks a lot Lol… I’ve got it resolved quick!
.