rewrite url to subdirectory - apache

I'm new at url rewriting using appache.
I have a htaccess file with those rules:
RewriteCond %{HTTP_HOST} ^hostname/* [NC]
RewriteRule http://hostname/subDir/* [L,R]
The purpose of the rules is for rewriting the request from http://hostname to http://hostname/subDir.
I tested the rules using http://htaccess.mwl.be/ but cannot make the RewriteRule work. Tried different rules but non of them works.
Can someone help me with this simple rule?

It is a bit unclear what exactly do you want to achieve, but something like this should work:
RewriteEngine On
RewriteCond %{HTTP_HOST} =www.example.com [NC]
RewriteCond %{REQUEST_URI} !^/subDir [NC]
RewriteRule ^ subDir%{REQUEST_URI} [L]

Related

Redirecting all but one pattern of file to new site

If I have http://example.com and I want to redirect all traffic except orange.1.php, orange.2.php, orange.32.5.php, etc to http://newexample.com, how would I write the .htaccess? Just to be clear, http://example.com/blah should redirect to http://newexample.com, not to http://newexample.com/blah. I've googled for an hour trying different things, and none seem to work.
I thought this should work:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteCond %{REQUEST_URI} !^orange\.[0-9]+.*$
RewriteRule ^/?.*$ "http:\/\/newexample\.com" [R=301,L]
Thanks!
REQUEST_URI always starts with a forward slash.
You can use this refactored shorter rule:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteRule !^orange\.\d+\.php$ http://newexample.com/? [R=301,L,NC]

htaccess SEF URL rewriting not working with question mark

Please I'm facing some challenges with my URL rewriting.
I'm trying to rewrite:
domian.com?v=subj to domian.com/subj
and
domian.com?v=subj&id=3 to domian.com/subj/id
So users can directly access a clean URL.
Here's what I tried that wasn't working:
RewriteCond %{QUERY_STRING} ^v=(.*)$
RewriteRule ^index.php/?$ $1? [NC]
I also tried this too:
RewriteRule ^(.*)/([0-9])$ http://domain.com/$1/$2 [NC]
Both were not working. Please I need some clarification on this. Thanks.
You can use the following 2 rules to redirect query strings to SEF format.
RewriteEngine on
#--Redirect "?v=foo&id=bar" to "/foo/bar"
RewriteCond %{THE_REQUEST} /index\.php\?v=([^&]+)&id=([^&\s]+) [NC]
RewriteRule ^ /%1/%2? [NC,L,R]
#--Rewrite "/foo/bar" to "?v=foo&id=bar"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?v=$1&id=$2 [NC,QSA,L]

htaccess is redirecting seems to be ignoring not rewritecond

I have the following scenario and I've spent hours trying to work out why it's redirecting.
Here's my htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/mypage/?$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^mypage/?$ /testing.php [NC,L]
When I browse to http://www.example.com/mypage it is redirecting to https://www.example.com/mypage
I would expected because of the !^/mypage/?$ condition it wouldn't, however, I seem to be completely wrong!
Can someone please put me out of my misery and let me know why this isn't working?
Thanks in advance.
It is because 2nd rule is rewriting /mypage to /testing.php and your REQUEST_URI becomes /testing.php in the 2nd loop of mod_rewrite.
Have your rules like this:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/(mypage/?|testing\.php)$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
RewriteRule ^mypage/?$ /testing.php [NC,L]
Also make sure to test it in a new browser to avoid 301 caching issues.

htaccess redirect and rewrite rules conflicting

I want to rewrite all requests to index.php?r=
So that /sth becomes /index.php?r=sth
I have applied this rule that works:
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteCond %{REQUEST_URI} !^.*\.(jpg|css|js|gif|png|pdf|php)$ [NC]
RewriteRule ^(.+)$ index.php?r=$1 [L]
However I want to also have redirects such as
Redirect 301 /sth /sth-new
What happens is that it works but the url becomes:
/sth-new?r=sth
Do you have any solutions - suggestions about why this is happening?
How can I implement a global rule and also have simple redirects?
You can do it like this:
RewriteRule ^sth/?$ /sth-new? [L,R=301]
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond %{REQUEST_URI} !\.(jpg|css|js|gif|png|pdf|php)$ [NC]
RewriteRule ^(.+)$ index.php?r=$1 [L]
You should keep external redirect rules before your internal rewrite rules.

Redirect Subdomain to new domain

Hi guys trying to get a 301 redirect working and having trouble. I need to redirect sub.domain1.com to www.domain2.com and make sure that any file names or parameters get sent over with it.
This is what I was trying:
RewriteCond %{HTTP_HOST} ^domain1.com [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [L,R=301]
I also tried this:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^sub\.domain1\.com$ /www.domain2.com? [R=301,NE,NC,L]
Where am I messing up?
You missed the subdomain part and proper escaping.
RewriteCond %{HTTP_HOST} ^sub\.domain1\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [L,R=301]
Further explain can be found in this question.
Rule of thumb for rewriterules: from the most complex to the less complex.
And don't forget the QSA directive (QSA = Query String Append = "make sure that any file names or parameters get sent over with it")
RewriteCond %{HTTP_HOST} ^sub\.domain1\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [QSA,R=301,L]
Tell me if it works.