.htaccess RewriteRule redirect - apache

i need help redirecting this two links to one link:
Link #1: http://www.domain.com/index.php?til=d_news&id_new=1
Link #2: http://www.domain.com/new_folder/?til=d_news&id_new=1
need to redirect to:
http://www.domain.com/news/news.html
i tried using this code:
RewriteCond %{QUERY_STRING} ^til=d_news
RewriteRule ^(.*)$ http://%{HTTP_HOST}/news/news.html? [R=301,L]
but it only redirects the first link and not the second.

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^til=d_news&id_new=1 [NC]
RewriteRule ^ /news/news.html? [R=301,L]

In the htaccess file in your document root, add:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^til=d_news&id_new=1
RewriteRule ^index.php$ /news/news.html? [L,R=301]
RewriteCond %{QUERY_STRING} ^til=d_news&id_new=1
RewriteRule ^new_folder/?$ /news/news.html? [L,R=301]

In your second link there is no destination page:
http://www.domain.com/new_folder/?til=d_news&id_new=1
should be something like:
http://www.domain.com/new_folder/page.php?til=d_news&id_new=1

Related

mod rewrite redirect all to https except one file

I would like to redirect all my pages so they use the HTTPS protocol except one path. I did find the following page ( mod_rewrite redirect all to https except one file ), but the solution provided there didn't work for me. I have also tried other solutions, all to no avail.
Here is my current code:
Options -Indexes
RewriteEngine On
# do nothing for my-awesome-path.php
RewriteRule ^keg-collars-studio\.php$ - [L]
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI}
RewriteRule ^(.*)\.html$ $1\.php [QSA,L]
I have edited my post with the exact content and path. Any help is greatly appreciated.
Try this .htaccess:
Options -Indexes
RewriteEngine On
# Turn SSL off everything but payments
RewriteCond %{THE_REQUEST} !/keg-collars-studio\.php [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=302]
RewriteRule ^(.+?)\.html$ $1.php [NC,L]

.htaccess if subdomain show www.site.com/subs/. no redirect url must bu sub.site.com

I have problem with .htaccess.
I need:
if $_SERVER["HTTP_HOST"] is sub.site.com must show content from www.site.com/sub/. But URL must be like sub.site.com.
And if sub.site.com/content/ must show content from www.site.com/sub/content/
Is it Possible?
Setting subdomains from hosting not working for me because of my CMS.
Try adding these rules to the htaccess file in your document root:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub.site.com$ [NC]
RewriteCond %{DOCUMENT_ROOT}/sub%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/sub%{REQUEST_URI} -d
RewriteRule ^(.*)$ /sub/$1 [L]
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^sub\.site\.com$ [NC]
RewriteRule (?!^sub/)^(.*)$ /sub/$1 [L,NC]

Redirecting from subdomain without changing URL

I want to redirect a subdomain with the .htaccess file without changing the URL. But can't figure it out.
ex. i want to turn
http://foo.domain.com
into
http://www.domain.com/users/foo/dashboard.php
I generated a script to do this, but the URL doesn't stay on http://foo.domain.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com [NC]
RewriteRule ^ http://www.domain.com/users/%1/dashboard.php [L]
Thanks for the help!
What you're htaccess it is doing is simply saying "if you find this, do a full redirect to that url"
Do this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com [NC]
RewriteRule ^ dashboard.php?domain=%1 [L, QSA]
And it should work (moving to wherever dashboard is in your directory structure on-server, relatively)
Try this (assuming that all the subdomains point to the root directory) :
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^.*$ users/%1/dashboard.php [L]
Proxy flag [P] will do the trick:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.org$ [NC]
RewriteRule http://example.org/users/%1/dashboard.php [P,NC,QSA]
</IfModule>

Two level url rewrite with mod_rewrite

I need something like this index.php?page=home to be /home
and
index.php?page=products&cat=chairs to be /products/chairs
What would be the mod_rewrite rule to achieve this?
Edited
I have other pages than home, so my full content of .htaccess file is now
RewriteEngine On
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [L]
RewriteRule ^/products(:/([^/]+))? /index.php?page=products&cat=$1 [L]
But when I navigate to /products/chairs I get a "Object not found" error.
RewriteEngine on
RewriteRule ^/home/?$ /index.php?page=home [L]
RewriteRule ^/products(:/([^/]+))? /index.php?page=products&cat=$1 [L]
It rewrites both /home and /home/, both /products and /products/ (you didnt specify which was your intended behavior); beware that you will get an empty cat parameter in those cases.
EDIT: this is a tested .htaccess, with R=301 left on it to see rewriting result, with both .htaccess and index.php in same directory (webroot in my case):
RewriteEngine on
RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [R=301,L]
RewriteRule ^products/([^/]+)?/? index.php?page=products&cat=$1 [R=301,L]
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?page=(products)&cat=([^\s]+) [NC]
RewriteRule ^ %1/%2 [R=302,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+index\.php\?page=(home)\s [NC]
RewriteRule ^ %1 [R=302,L]
RewriteRule ^(products)/([^/]+)/?$ index.php?page=(products)&cat=$1 [L,QSA,NC]
RewriteRule ^(home)/?$ index.php?page=$1 [L,QSA,NC]
Change R=302 to R=301 after you verify it's working fine for you.

.htaccess redirect root to index.php

I need to redirect from http://example.com/ to http://example.com/index.php.
Use this:
DirectoryIndex index.php
Try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^$
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^$ http://example.com/index.php [L,R=301]
Just to add my own solution, since the answer of Michael did not work for me, I did something like:
RewriteEngine on
# These two lines redirect non-www to www.
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) https://www.example.com$1 [R=301,L]
# These two lines redirect the root to index.html.
RewriteRule ^$ /index.html [R=301,L]
RewriteRule ^/$ /index.html [R=301,L]
This also preserves any possible existing query string parameters.
Adding to #Uwe Keim's answer above, I had non-www to www setup on Apache virtual hosts.
What worked for me on .htaccess was:
RewriteEngine on
# Two lines to redirect from the root web directory to myfile.html.
RewriteRule ^$ /index.html [R=301,L]
RewriteRule ^/$ /index.html [R=301,L]