Safely move my blog? - .htaccess

My blog is currently located at:
example.com/blog
with posts being something like:
example.com/blog/my-post
I want to edit my htaccess file so anyone that accesses
example.com/blog/my-post
would end up at:
example.com/my-post
How can I do this with an htaccess file?
UPDATE
Right now I have:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# www.
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
</IfModule>
That will forward any non-www traffic to www. However, I need to modify this to support /blog as well. I don't think my syntax is right:
RewriteCond %{HTTP_HOST} ^example\.com/blog$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
?

You cannot match /blog using HOST_NAME variable.
Have your htaccess like this:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# www.
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301,NE]
RewriteRule ^blog/(.*)$ /$1 [L,NC,R=301]
</IfModule>
This assumes there is no .htaccess in /blog/ directory. However if it is there for some reason then use this rule as first rule in /blog/.htaccess:
RewriteEngine On
RewriteRule ^(.*)$ /$1 [L,R=301]

Related

Issue While redirecting from non www URL to www URL

Hi On our website we are facing a issue like when we enter url without www for eg. example.com/xyz in this case after redirect it is converting to www.example.com/?url=xyz but it should be redirect to simply like: www.example.com/xyz
So we are getting extra ?url= in URL
We have used code for redirect non www url to www in our htaccess file. Code for redirect in htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
We have also tried this code but not work:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
</IfModule>
Try placing your redirect rule before the routing rules:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>

Rewrite Role does not apply

I have php website and I have created .htaccess file. I have two rules. One for force www and other for permalink.
I want to change following Url
http://www.yatha.tv/play.php?vid=1437&id=1
to
http://www.yatha.tv/1437/1.html
But this rule does not change
RewriteRule ^([^/]*)/([^/]*)\.html$ /play.php?vid=$1&id=$2 [L]
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^yatha\.tv$
RewriteRule ^(.*)$ http://www.yatha.tv/$1 [R=301,L]
RewriteRule ^([^/]*)/([^/]*)\.html$ /play.php?vid=$1&id=$2 [L]
</IfModule>

Redirecting www directory to non www https directory

The code below redirect mydomain.com/webmail to https://mydomain.com:8080/webmail
<IfModule mod_rewrite.c>
<IfModule mod_ssl.c>
<Location /webmail>
RewriteEngine on
RewriteCond %{HTTPS} !^on$ [NC]
RewriteRule . https://%{HTTP_HOST}:8080%{REQUEST_URI} [L]
</Location>
</IfModule>
</IfModule>
The code worked but I want to make slight modification. I want to redirect www.mydomain.com/webmail to https://mydomain.com:8080/webmail
How can I do that?
Change the rule to:
RewriteEngine on
RewriteCond %{HTTPS} !^on$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?(.*)$ [NC]
RewriteRule . https://%2:8080%{REQUEST_URI} [L]

rewrite rule for shop in lithium / apache

I have a lithium installation and all the .htaccess works fine.
I need to install OpenCart as a shopping cart in app/webroot/shop
I copied all the files and also changed the .htaccess file in the root folder of lithium installation as
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule shop/(.*) /app/webroot/shop/$1 [L]
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
Still when I browse http://domain.com/shop it takes me to http://domain.com/app/webroot/shop/
With an error on page:
Exception
lithium\action\DispatchException (code 404)
Action `webroot` not found.
Please help me in solving this problem.
You may try this instead:
<IfModule mod_rewrite.c>
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !/app/webroot/shop/? [NC]
RewriteRule ^shop/(.*) /app/webroot/shop/$1 [L,NC]
RewriteCond %{REQUEST_URI} !/app/webroot/? [NC]
RewriteRule ^$ /app/webroot/ [L,NC]
RewriteCond %{REQUEST_URI} !/app/webroot/? [NC]
RewriteRule ^(.*) /app/webroot/$1 [L,NC]
</IfModule>
The problem is that your last "catch all" rule is also redirecting all requests for the shop to lithium, which you don't want.
Try this
Options +FollowSymlinks -MultiViews
RewriteEngine On
# Rewrite all URLs that start with /shop to /app/webroot/shop
RewriteRule ^shop/.? /app/webroot/shop%{REQUEST_URI} [L]
# Rewrite all URLs that don't start with /app/webroot/shop to /app/webroot
RewriteCond %{REQUEST_URI} !^/app/webroot/shop
RewriteRule .? /app/webroot%{REQUEST_URI} [L]

Codeigniter url rewrite like subdomain url with .htaccess

Currently my Url is: http://www.domain.co.uk/index.php/city/details/city-name
I would like to change it to:
http://www.city-name.domain.co.uk/index.php/city/details/city-name
or:
http://www.city-name.domain.co.uk/city/details/city-name
Put the .htaccess file into the http ://www.domain.co.uk/ document root
To http ://www.city-name.domain.co.uk/index.php/city/details/city-name
RewriteRule ^(.*)/([^/]+)$ http://www.$2.domain.co.uk/$1/$2 [R=301,L]
To http ://www.city-name.domain.co.uk/index.php/city/details/city-name
RewriteRule ^index.php/(.*)/([^/]+)$ http://www.$2.domain.co.uk/$1/$2 [R=301,L]
If the server is the same, set above RewriteRule this line to prevent redirection loop
RewriteCond %{HTTP_HOST} !^www\.(.*).domain\.co\.uk [NC]
File content example
<ifModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.(.*).domain\.co\.uk [NC]
RewriteRule ^index.php/(.*)/([^/]+)$ http://www.$2.domain.co.uk/$1/$2 [R=301,L]
</IfModule>
To exclude domain.co.uk (whitout www)
<ifModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.(.*).domain\.co\.uk [NC]
RewriteCond %{HTTP_HOST} !^domain\.co\.uk [NC]
RewriteRule ^index.php/(.*)/([^/]+)$ http://www.$2.domain.co.uk/$1/$2 [R=301,L]
</IfModule>

Resources