I am using mod_rewrite in .htaccess to redirect from .php to .html like this:
RewriteRule ^([^.]+).html$ /$1.php [QSA,L]
It works in a web browser, but Google is crawling the old URLs i.e. index.php and faq.php.
How I can redirect the index.php to index.html? My website URL is: http://www.21flats.com/.
Add [QSA,L,R=301] so Google will see that there has been a redirect. This adds a 301 redirect to the rewrite for search engines:
RewriteRule ^(.*)\.php$ /$1.html [QSA,R=301,L]
You have the redirect backwards, so change it and add the 301 rule and you are good.
You can try something like this:
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)\.html$ $1.php [L]
Related
I want to redirect below two dynamic url from old url's to new url's using .htaccess and i have done this application using codeigniter.
1) This are the dynamic url's and there are more than thousands of url's in my database.
Old(From) URL
https://www.example.com/area-name/pangothe-263001
New(To) URL
https://www.example.com/pangothe-263001
2) This are the amp version of the above url.
Old(From) URL
https://www.example.com/amp-area-name/pangothe-263001
New(To) URL
https://www.example.com/pangothe-263001/amp
I have tried with below code
RewriteRule ^area-name$ http://www.example.com/area-name [R=301,L]
RewriteRule ^amp-area-name$ http://www.example.com/amp-area-name [R=301,L]
But not redirecting to the new urls. How can do this redirect using .htaccess.
Keep these 2 redirect rules just below RewriteEngine line:
RewriteEngine On
RewriteRule ^area-name/(.+)$ /$1 [R=301,L,NC,NE]
RewriteRule ^amp-area-name/(.+)$ /$1/amp [R=301,L,NC,NE]
# remaining rules go below this
Try This
RewriteRule ^area-name/pangothe-263001/?$ $1/pangothe-263001$2 [R=301,L]
Try with below rules,
Canonical links Redirect
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/area-name/(.*)$
AMP links Redirect
RewriteRule ^ https://www.example.com/%1 [R=301]
RewriteCond %{REQUEST_URI} ^/amp-area-name/(.*)$
RewriteRule ^ https://www.example.com/%1/amp [R=301]
I'm trying to redirect a folder and all its sub files to a URL with a .htaccess file.
But
Redirect 301 /abc/cba/ http://www.aaa.com/
Will make /abc/cba/ddd/index.html redirect to http://www.aaa.com/ddd/index.html
What I want is redirect /abc/cba/ /abc/cba/ddd/index.html to http://www.aaa.com/
Could anyone help? Thanks. If anything not clear, please let me know.
By default, Redirect sort of maps the path node to a new path node, so anything after the first path gets appended to the target URL.
Try:
RedirectMatch 301 ^/abc/cba/ http://www.aaa.com/?
Or if you'd rather use mod_rewrite instead of mod_alias:
RewriteEngine On
RewriteRule ^/?abc/cba/ http://www.aaa.com/? [R=301,L]
here's another example of a mod_rewrite rule that worked for me
I wanted to redirect a sub directory to the root of the same domain.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^sub_directory/(.*)$ /$1 [R=301,NC,L]
</IfModule>
more examples can be found here:http://coolestguidesontheplanet.com/redirecting-a-web-folder-directory-to-another-in-htaccess/
I perfer the following method:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/somedir [NC]
RewriteRule /(.*) http://somesite.com/lost/$1 [R=301,L]
I had to reroute urls from old site version to new version, so here is what I did to reroute any links from about-us/* to about-us.html
RewriteEngine on
RewriteRule ^about-us/(.*)$ about-us.html [R=301,L]
What it doesn't do is rewrite something like domain.com/about-us/thing.html => domain.com/about-us.html .
It does work for things without extensions domain.com/about-us/something-in-url => domain.com/about-us.html
I added the lines below to redirect .jpg and .png, but it didn't work for .html, I can't find out why.
RewriteRule ^about-us/(.*).jpg about-us.html [R=301,L]
RewriteRule ^about-us/(.*).png about-us.html [R=301,L]
I have an existing site with php in the URL indexed. I want to change the URLs to not include PHP so thepage.php will be rewritten to http://example.com/thepage
The rewrite is working, but the redirect causes a 'page can't be displayed in a loop' error when I try to access the page as using thepage.php
I need to figure out how to setup the redirect and also rewrite the URLs, because Google has now indexed the PHP URLs and the pretty URLs.
I've looked at countless pages online, but can't find the answer.
Please help.
Here's my htaccess
# The redirect below caused a loop when I access the page as
# http://example.com/thepage.php
# Moving the redirect after the rewrite didn't work either.
Redirect 301 /thepage.php http://example.com/thepage
RewriteEngine on
# Rewrite works. The page does display corectly for this link
# http://example.com/thepage
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
# RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
#RewriteRule ^photos([^\.]+)$ photos [NC,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>
ErrorDocument 404 http://example.com/
Thank You
Your rewrite rule is backward from the sounds of it.
Here we're capturing all php files that do no contain periods and redirecting them to a directory URI using only the name without the extension. [R=301] is a permanent 301 redirect that tells whoever that the page is no longer in its old place.
RewriteEngine on
RewriteRule ^([^\.]+).php$ $1 [NC,L,R=301]
You might want to test these with R only without the 301 to make sure it's working. If you do a 301 redirect that doesn't work it will be cached in the end user's browser cache until it expires.
Apache has an extensive reference on remapping URLs with redirects.
I finally was able to get back to this problem and found the solution. I hope this can help somebody else. I tore my hair out on this problem.
I found many examples but not with the combination that I needed
#Force non-www:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301,NC]
RewriteBase /
# hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+).php [NC]
RewriteRule ^ %1 [R,L,NC]
# To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]
ErrorDocument 404 http://example.com/
I am updating an old asp site to cakephp - the old site has various listings on google based on the old "filename.asp" urls - I'd like to put Redirect 301s in the htaccess file to try and hang on to those search results (most of the pages have a complementing page on the new site), but something appears to be going wrong. htaccess as follows (excluding standard cake stuff). What am I doing wrong?
Redirect 301 contact.asp /contact
Redirect 301 portfolio.asp /portfolio-design-web
Redirect 301 webhosting.asp /
I've tried with the htaccess in the root directory, and webroot but it should just work wherever, no?
--
fixed it using mod_rewrite, following rules inside .htaccess on webroot work:
RewriteRule ^contact.asp$ /contactos/ [R=301,L]
Try modifying app/webroot/.htaccess like so:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^contact.asp$ /contact [R=301,L]
RewriteRule ^portfolio.asp$ /portfolio-design-web [R=301,L]
RewriteRule ^webhosting.asp$ / [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
I have a online store that, for example, is located here: hxxp://domain.com/store/
Within the store directory's .htaccess file I have this:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /store/
RewriteCond %{HTTP_HOST} !^www.domain.com$
RewriteRule ^(.*)$ http://www.domain.com/store/$1 [R=301]
RewriteRule ^/?$ directory.php
RewriteRule ^search/?$ search.php
RewriteRule ^category/([a-zA-Z0-9\!\#\#\$\%\^\&\*\(\)\?\_\-\ ]+)/?$ product.php?CategoryID=$1 [QSA]
RewriteRule ^([a-zA-Z0-9\!\#\#\$\%\^\&\*\(\)\?\_\-\ ]+)/?$ product/detail.php?ProductID=$1 [QSA]
It works great!
My issue (problem) is that I now need to change the /store/ directory to /shop/ which seemed simple enough. However, I need to setup proper 301 redirects so that I don't loose any SE rankings that I may have.
What is the best way to setup 301 redirects in this situation?
The only solution I have found is to setup a redirect 301 for each category, product etc. in the store. Like so, for example.
Redirect 301 /store/category/sample-widgets/ hxxp://www.domain.com/shop/category/sample-widgets/
This works and does what I need it to, but... the URL in the address bar displays like so: hxxp://www.domain.com/shop/category/sample-widgets/?CategoryID=sample-widgets
I can not figure out why or how to remove the query string.
Please help. Thanks.
You can handle the 301 errors by using a PHP script to handle the redirects.
In your .htaccess file you would add this rule:
Redirect 301 /error301.php
Create the file error301.php:
<?php
$redirects = array('/path/to/old/page/' => '/path/to/new/page/',
'/store/category/sample-widgets/' => '/shop/category/sample-widgets/');
if (array_key_exists($_SERVER['REQUEST_URI'], $redirects))
{
$dest = 'http://'.$_SERVER['HTTP_HOST'].$redirects[$_SERVER['REQUEST_URI']];
header("Location: {$dest}", TRUE, 301); // 301 Moved Permanently
}
You could use a simple Redirect directive like:
Redirect 301 /store/ /shop/
Or, if you want to use mod_rewrite as well, you would need to change your current rules as you can not use the base URL /store/ anymore:
RewriteEngine on
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,R=301]
RewriteRule ^store/?([^/].*)?$ /shop/$1 [L,R=301]
RewriteRule ^shop/?$ directory.php
RewriteRule ^shop/search/?$ shop/search.php
RewriteRule ^shop/category/([a-zA-Z0-9!##$%^&*()?_\-\ ]+)/?$ product.php?CategoryID=$1 [QSA]
RewriteRule ^shop/([a-zA-Z0-9!##$%^&*()?_\-\ ]+)/?$ product/detail.php?ProductID=$1 [QSA]