301 redirect from .htm to / - .htaccess

I have just migrated to a WordPress website and my old site had URLs ending in .htm eg https://www.example.com/accessories.htm and I would like to 301 redirect all these URLs to root ie https://www.example.com/accessories/ - I have tried the following code in htaccess but it doesn't work
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^\.htm$ /? [R=301,NE,NC,L]

Try this rule as your topmost rule:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/(\S+)\.html?\s [NC]
RewriteRule ^ /%1/ [L,NE,R=301]
# remaining rules go here

Related

How to redirect one url to another url using .htaccess?

I am trying to redirect old url to new url but it is not redirecting.
Old url: http://domain.com/STACK/App/
New url: http://domain.com/stack-app
And my .htaccess file
Redirect 301 /STACK/App http://domain.com/stack-app
Thanks,
Try this code.
RewriteEngine on
RewriteRule http://domain.com/STACK/App/ /http://domain.com/stack-app [R=301,L]
For more help go to this link:--
http://edward-designer.com/web/htaccess-url-rewrite-simplified/
You can use this rule as your very first redirect rule in your root .htaccess:
RedirectMatch 301 ^/STACK/App/?$ /stack-app
Within the mod_rewrite section of your .htaccess file, add the following lines on the old site:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com\STACK\App\$
RewriteRule (.*)$ http://domain.com/stack-app/$1 [R=301,L]
</IfModule>
you can use the following code to redirect your website using .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} !oldexample.com$ [NC]
RewriteRule ^(.*)$ http://www.newexample.com/$1 [L,R=301]
for non www version, you can use the following,
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !oldexample.com$ [NC]
RewriteRule ^(.*)$ http://newexample.com/$1 [L,R=301]

Rewrite rule to work consistently for subdomain and subdirectory

I have a nice rule for rewriting the url to /meetings, but the rule doesn't rewrite the url properly when visited from a subdirectory.
EDIT: My .htaccess file is in the subdirectory /blog
When I use this rule:...
RewriteRule ^meetings$ /?page_id=2809 [L]
When visited from subdomain(blog.example.com/meetings): blog.example.com/meetings
When visited from subdirectory(example.com/blog/meetings): 404 error
When I use this rule:...
RewriteRule ^meetings$ http://blog.example.com/?page_id=2809 [L]
When visited from subdomain(blog.example.com/meetings): blog.example.com/meetings
When visited from subdirectory(example.com/blog/meetings): blog.example.com/?page_id=2809
Is there a way to get the rule to work consistently for the subdomain and the subdirectory?
You may try this in one .htaccess file in /blog directory:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /blog
RewriteCond %{HTTP_HOST} blog\.example\.com [NC]
RewriteCond %{QUERY_STRING} !page_id= [NC]
RewriteRule ^meetings /?page_id=2809 [L,NC]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteCond %{QUERY_STRING} !page_id= [NC]
RewriteRule ^meetings /blog/?page_id=2809 [L,NC]
For permanent redirection, replace [L,NC] with [R=301,L,NC].

301 Redirects with dynamic URLs

I am trying to rewrite my .htaccess file to redirect old dynamic URLS to new ones.
I have tried these so far but they are not working as expected. The second rule seems to redirect to the first one.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mydomain.com$
RewriteRule ^(.*)$ "http\:\/\/www\.mydomain\.com\/$1" [R=301,L]
Redirect 301 /product.php?l=old-product-name1 http://www.mydomain.com/product.php?l=new-product-name1
Redirect 301 /product.php?l=old-product-name2 http://www.mydomain.com/product.php?l=new-product-name2
Can anyone help me redirect these properly?
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^mydomain.com$
RewriteRule ^(.*)$ "http\:\/\/www\.mydomain\.com\/$1" [R=301,L]
RewriteCond %{QUERY_STRING} =l=old-product-name1
RewriteRule ^product.php$ http://www.mydomain.com/product.php?l=new-product-name1 [R=301]

(.htaccess) 301 redirect with same url structure

I use 301 redirect from non-WWW to WWW domain! Works fine, but links are different.
Example:
from - ggkj.com/posts/godzilla/43
to - www.ggkj.com/index.php?view=posts&author=godzilla&aid=43
my .htaccess 301 redirect code:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*) http://www.example\.com/$1 [L,R=301]
.htaccess code: http://pastebin.com/t3FA59uq
This is currently what i use on my website with success:
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{http_host} !^www.walterbax.ca [NC]
RewriteRule ^(.*)$ http://www.walterbax.ca/$1 [R=301,L]
EDIT: after looking into your question a little further I can tell you that your issue is improperly placed in the file. it must be hitting another rule that is rewriting it into URL variables.
I would start with a basic htaccess for the www. rule for testing and add to it from there.
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^yourdomain.com$
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [R=301,L]

htaccess for www-only redirection

Is there any way I can redirect my urls to only use http://www.domain.com instead of http://domain.com?
Here's my htaccess but doesnt work:
# Turn on URL rewriting
RewriteEngine On
# Installation directory
# RewriteBase /test/
Options +FollowSymlinks
# Protect application and system files from being viewed
RewriteRule ^(application|modules|system) - [F,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT,L]
#redirec
#detect urls without www
RewriteCond %{http_host} ^thehotelinventory.com [NC]
#redirect to www, does not work though
RewriteRule ^(.*)$ http://www.thehotelinventory.com/$1 [R=301,L]
AddHandler php5-script .php
If you need to redirect from non-www to www, then redirect to HTTPS for certificates like THAWTE that dont support www non-www on SSL 123.
RewriteEngine On
#non-www to www
RewriteCond %{HTTP_HOST} ^yourdomain.com
RewriteRule (.*) http://www.yourdomain.com/$1 [R=301,L]
#Redirect to HTTPS before www redirect
RewriteEngine on
RewriteCond %{HTTPS}
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Thanks to Brasil na Web Dev. Guys.
a simple answer can be found here: http://snippets.dzone.com/posts/show/2264

Resources