Conditional Redirect with htaccess - php

I have a site: blog.example.com.
I want blog.example.com to go to example.com/news
But I want blog.example.com/tag/mytag or blog.example.com/category/mycategory to go to example.com/tag/mytag or example.com/category/mycategory
So far I have my .htaccess like this but of course it doesnt work:
RewriteEngine on
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteRule ^(.*)$ http://www.example.com/news/ [R=301,L]

You can use this in root .htaccess of blog.example.com:
RewriteEngine on
RewriteRule ^/?$ http://www.example.com/news [R=301,L]
RewriteRule ^(.+)$ http://www.example.com/$1 [R=301,L,NE]

Related

Rewrite htaccess to point to a folder

The below is original .htaccess, i want to modify it to point to a different folder such as www.yourdomain.com/clients/foldername. How do i do it?
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
do let me know what i should do that it point to that particular folder. Should i write like this
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.yourdomain(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
do help me
This should take care of it.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ your/new/directory/$1 [R=301,L]
This will match all requests. If you only want to redirect certain folders to another you will need to change the RegEx on the RewriteRule.
RewriteRule ^some/path/to/redirect(rest/of/the/uri)$ your/new/directory$1 [R=301,L]
To redirect to a different domain use a RewriteRule like this.
RewriteRule ^(.*)$ http://the.newdomain.tld/your/new/directory/$1 [R=301,L]

htaccess get other variables

This is my whole .htaccess code:
RewriteEngine on
Options +FollowSymlinks
Header set X-UA-Compatible "IE=Edge,chrome=1"
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]
RewriteCond %{REQUEST_URI} !(/$|\.)
RewriteRule (.*) %{REQUEST_URI}/ [R=301,L]
RewriteRule admin/^([^\.]+)$ ./admin.php?get=$1 [L,QSA]
RewriteRule admin/ ./admin.php [L,QSA]
php_value date.timezone "Europe/Bratislava"
I want that, when users are at http://something/admin/ it will show up admin.php - that is this row RewriteRule admin/ ./admin.php [L,QSA] and it works well.
But when users are at http://something/admin/profile/email/ it should send something like admin.php?get=profile/email/, but it doesn't. What is wrong?

.htaccess POST data blocked

I send POST data from a form to my root index that will redirect :
<form action="index.php?menu=blog&section=admin" method="post">
But this lines in .htaccess blok POST data :
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*) http://www.example.com/$1 [QSA,L]
I'v tried a few other ones but same problem :
#RewriteRule ^(.*) http://www.example.com/$1 [QSA,L,R=301]
#RewriteRule (.*) http://www.example.com/$1 [QSA,L,R=301]
#RewriteRule ^(.*) www.example.com/$1 [QSA,L,R=301]
#RewriteRule ^(.*) http://www.example.com/$1 [QSA,NC,P] #Doesn't rewrite "www"
There are a few other post with similar problem but I don't find my solution
How can I send POST datas to index.php?menu=blog&section=admin ?
Thank you !
Try these rules
Rewriting product.php?id=12 to product-12.html
RewriteEngine on
RewriteRule ^product-([0-9]+)\.html$ product.php?id=$1
Rewriting product.php?id=12 to product/ipod-nano/12.html
RewriteEngine on
RewriteRule ^product/([a-zA-Z0-9_-]+)/([0-9]+)\.html$ product.php?id=$2
Redirecting non www URL to www URL
RewriteEngine On
RewriteCond %{HTTP_HOST} ^viralpatel\.net$
RewriteRule (.*) http://www.viralpatel.net/$1 [R=301,L]
Rewriting yoursite.com/user.php?username=xyz to yoursite.com/xyz
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?username=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?username=$1
Redirecting the domain to a new subfolder of inside public_html
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.test\.com$
RewriteCond %{REQUEST_URI} !^/new/
RewriteRule (.*) /new/$1

htaccess - Why aren't my rewrite rules working?

There's some misconfiguration in my htaccess which I can't solve :/
Here's the htaccess:
RewriteEngine on
RewriteRule ^([^\.]+)/?$ index.php?page=$1
rewritecond %{http_host} ^domain.com [NC]
rewriterule ^(.*)$ http://www.domain.com/$1 [r=301,L]
The .htaccess actually works fine, except in one case: When I connect to a subpage to the root domain without "www.", like this:
http://domain.com/somestuff
Then the first rule doesn't apply and I get redirected to:
http://www.domain.com/index.php?page=somestuff
What have I done wrong?? Thanks for any suggestions!
Try moving
RewriteRule ^([^\.]+)/?$ index.php?page=$1
below
RewriteRule ^(.*)$ http://www.domain.com/$1 [r=301,L]

apache rewrite rules issue for specific pages

I have problem with my .htaccess redirections. When I type:
http://www.domain.com/contact
it goes to the index.php and not the contact.php
here's my .htaccess:
Redirect 301 /clients http://clients.domain.com
RewriteEngine On
SetEnvIf Host ^www\. page=www
SetEnvIf Host ^mob\. page=mobile
RewriteBase /
SetEnvIfNoCase User-Agent "^Wget" bad_bo
#etc ...
Deny from env=bad_bot
RewriteCond %{HTTP_HOST} !^www.domain.com
RewriteRule (.*) http://www.domain.com/$1 [R=301]
RewriteRule ^about/?$ about.php
RewriteRule ^contact/?$ contact.php
rewriterule ^(.*)$ index.php?subdomain=%{ENV:page}&page=$1
in my php i get:
<?php
print_r($_GET);
Array (
[subdomain] => www
[page] => contact.php
)
What am I missing?
Try this rule:
RewriteCond %{HTTP_HOST} !^www.domain.com [NC]
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
RewriteRule ^about/?$ about.php [NC,QSA,L]
RewriteRule ^contact/?$ contact.php [NC,QSA,L]
rewriterule ^([a-z0-9]+)$ index.php?subdomain=%{ENV:page}&page=$1 [NC,QSA,L]
I also added the NC, QSA, L flags to make sure the last rule [L] is executed if match, [NC] for non case and [QSA] for Query String Append.
You need to rewrite your about and contact rules in the following way:
RewriteRule ^about/?$ about.php [L,QSA]
RewriteRule ^contact/?$ contact.php [L,QSA]

Categories