Mod Redirect Oddity - mod-rewrite

I have a redirect working mostly but with one exception when a url is run without a forward slash the redirect shows in the address bar, this is not the behavior I'm expecting. Here is the .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(^.*)\.domain.com
RewriteRule (.*) redirector.php?subdomain=%1 [QSA,L]
now if there is a subdomain on my domain I want that request to go to the redirector.php file and this works in all cases unless I have a folder without a backslash. I've also tried putting in an optional forward slash at the end of the condition, still no go. So what happens when there is no forward slash at the end of a folder is that the request goes from
http://test2.domain.com/test
to
http://test2.domain.com/test/?subdomain=test2
cannot figure out how to get it to stop as
http://test2.domain.com/test/
will work.

ah figured it out you can easily check to see if the request doesn't have a file name and if so redirect with a forward-slash! So now I have:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]
RewriteCond %{HTTP_HOST} ^(^.*)\.domain.com
RewriteRule (.*) redirector.php?subdomain=%1 [QSA,L]

Related

Url Rewriting: Redirect all pages that don't start with a locale

I'm trying to redirect all pages that don't start with a locale to a page with a locale. For example: /index.htm is redirected to /en-US/index.htm, but /en-US/hello.htm isn't redirected anywhere.
I can't get the matching rule to work, this is what I tried:
!^/[a-z]{2}-[A-Z]{2}/$
Edit: jacouh was right, the thing worked the whole time, the rewrite rule was wrong, and so was my test.
I tested this in Debian/Apache2 in vhosts file:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{SCRIPT_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/en-US/hello\.htm
RewriteRule !^/[a-z]{2}-[A-Z]{2}/ /en-US/hello.htm [R=301,L]
that works.
/en-US/hello.htm must exist, otherwise, an infinite loop of redirection is possible.

Remove particular segment from url

I am using Codeigniter for one of my apps and have a following htaccess file
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]
Now i have a url which is like mydomain.com/group/username
For SEO reasons i am suppose to convert this url into mydomian.com/group/username into mydomian.com/username using 301 redirect
Also urls like mydomain.com/group/username/page1 should redirect to mydomian.com/username/page
The closest i have tried after googling is by pasting this below line at the end of the file
RewriteRule ^(.*?)/?group(.*)$ /$1 [L]
But it isnt working.. any idea where i have gone wrong ?
The answers on other questions doesnt seem to be working for me/ Am pretty bad with .htaccess
P.S - i tried this
RewriteRule ^group/username(.*)$ username/$1 [QSA,R=301,L]
It works fine but it gives consecutive slashes like mydomain.com/username//pagename
I have figured it out
RewriteRule ^group/username(.*)$ username$1 [QSA,R=301,L]
This is will simply remove the 'group' from your URL and will do 301 redirect
If
RewriteRule ^group/username(.*)$ username/$1 [QSA,R=301,L]
gives two slashes, change it to:
RewriteRule ^group/username/(.*)$ username/$1 [QSA,R=301,L]
The (.*)$ matches anything after username (including the slash) until the end.

Trouble redirecting subdomains to a folder

I have got this working:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.co\.uk
RewriteRule ^(.*) http://domain.co.uk/%1 [L]
So it will redirect test.domain.co.uk to test.domain.co.uk/test
However, I would like it so that it changes the document root rather than the user seeing the redirect. I have read a few articles on here and tried a few things. But I am not sure how to debug any errors, it just doesn't work. The one other that I have tried:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/-
RewriteCond %{HTTP_HOST} ^([^\./]+)
RewriteCond %{DOCUMENT_ROOT}/-%1 -d
RewriteRule ^(.*)$ -%1/$1 [L]
But this doesn't seem to work at all.
Any help would be appreciated.
Ian
Try something like this in your document root:
RewriteEngine On
# check if subdomain folder has the existing file, if so, serve it
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.co\.uk
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -f
RewriteRule ^(.*)$ /%1/$1 [L]
# check if subdomain folder has the existing directory WITH A TRAILING SLASH, if so serve it
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.co\.uk
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -d
RewriteRule ^(.*?)/$ /%1/$1/ [L]
# check if subdomain filder has the existing directory but missing trailing slash, redirect with it
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.co\.uk
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -d
RewriteRule ^(.*)$ /$1/ [R=301,L]
This method does 2 things for you, if it's a 404, it won't try to internally rewrite, so if you go to: http://sub.domain.co.uk/this/path/does/not/exist/blah, you won't get a 404 message saying: /sub/this/path/does/not/exist/blah doesn't exist, just /this/path/does/not/exist/blah because the URI wasn't rewritten, and thus not exposing your underlying directory structure.
The second thing is that you probably have DirectorySlash turned on. Which means if you access a directory like: http://sub.domain.co.uk/this/path, missing the trailing slash, mod_dir will want to redirect and add that slash, but it'll probably do it wrong because the URI has been rewritten and will redirect you to: http://sub.domain.co.uk/sub/this/path/. We you pre-emptively add the trailing slash with the correct URI hiding the sub.

mod_rewrite - redirect from one domain to another and preserve trailing values in url

I think this is a pretty straight forward question in mod_rewrite:
I got one domain, which needs to redirect to another, but keep any value after last slash (/) in the first URL, over to the second.
domain.com/4433 should transfer to domain.com/folder/?p=4333
Listed for clarity:
From: domain.com/4433
To: domain.com/folder/?p=4333
Any ideas?
Edit:
Did some testing, we found the following solution:
RewriteCond %{HTTP_HOST} ^domain.com$
RewriteRule ^([0-9a-z]*)$ /folder/?p=$1 [NC]
sincerely,
- bakkelun
In case you don't really want to redirect but to have pretty URLs, you can use
RewriteEngine On
RewriteRule ^/(.+)$ /folder?p=$1 [L]
This takes everything after the first slash and inserts it at the $1 - but only if there's something after the slash. It doesn't issue a redirect so the users won't notice.
Without any further information, try this:
RewriteEngine on
RewriteRule ^/([^/]+)$ /folder/?p=$1
If you want to use the rule in a .htaccess file, remove the leading slashes.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ domain.com/folder?p=$1 [R=301,L]
Just in case: domain.com = domain1.com and domain2.com? domain1.com should be redirected to domain2.com? Both run on the same server (optional)?
[EDIT:]
If you really only want to do the thing as stated in the comment, then do the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1.com$
RewriteRule ^4433$ http://domain2.com/folder/?p=4433 [R=301,L]
Else, as Benedikt Eger said, or with R=301 if you want real redirection.
Or, if you want it to redirect only on numbers, then do the following:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domain1.com$
RewriteRule ^([0-9])+$ http://domain2.com/folder/?p=$1 [R=301,L]
RewriteCond checks, if defined vhost is domain1.com, but not domain2.com, then the rewrite rule is applied, and redirects via HTTP status 301 [R=301] only number strings (0-9)+ consisting of at least one number to the specified URL. [L] makes this the last rule applied.

301 redirect

How do i redirect a url to domain .
eg. http://www.mydomain.com/index.php=HairThing --> http://www.mydomain.com
How do i redirect a non-www to www WITHOUT a slash at end ?
eg http://mydomain.com ---> http://www.mydomain.com
See also: Hidden features of mod_rewrite
#1
RewriteRule /index.php=HairThing$ http://www.mydomain.com [R=301]
#2
RewriteCond %{HTTP_HOST} ^mydomain.com
RewriteRule .* http://www.mydomain.com [R=301]
However, example case 1, as said by Greg, will always put the / on if it is without a uri.
mydomain.com # impossible
mydomain.com/ # possible
mydomain.com/foo #possible
mydomain.com/foo/ #possible
For your second question, the browser will always put a slash after the site name. This is because the trailing slash is required to indicate the root path of the web site.
you could use a general rule that works on every domain without having to change the name of the domain all the time. This is very helpful when you have multiple domains parked on same root.
RewriteCond %{HTTP_HOST} !^www\.[a-z0-9-]+\.[a-z]{2,6} [NC]
RewriteCond %{HTTP_HOST} ([a-z0-9-]+\.[a-z]{2,6})$ [NC]
RewriteRule (.*) http://www.%1/$1 [L,R=301]

Resources