Remove :8080 from url with htaccess? - .htaccess

When i type my url (without trailing slash!)
http://example.com/something
it redirects to
http://example.com:8080/something
while
http://example.com/something/
works fine.
Given that I cannot touch apache configuration it is possible to redirect
http://example.com:8080/something to http://example.com/something/
using only .htaccess in my site 'something' directory?
I've tried several rewrite rules, but none seems to work... In particular, I cannot get how to write the pattern matching the :8080 part.
Tried
RewriteCond %{SERVER_PORT} 8080
RewriteRule (.*) http://example.com/something/$1 [P,L]
but got Page not found error...
Btw, there's a specific common reason for apache to redirect to :8080 if there's no trailing slash?

Related

URL rewriting .htaccess help needed

Assume I have two domain names example1.com and example2.com. I forwarded example1.com to example2.com using the domain forwarding on hosting panel. It works fine but when I go to example1.com/sub it just shows example2.com as the URL. I want it to show example2.com/sub. I tried URL rewriting but no luck so far. It just keep loading and shows nothing. Am I missing something ?
This the rule I used.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^example1.com$ [NC]
RewriteRule (.*) http://www.example2.com/$1 [R=301]
First make sure that you've cleared your browser's cache. If it's a 301 redirect your browser will cache the redirect. Then make sure to turn off your hosting panel's forwarding.
Other than that, your rule should work fine, assuming it's at the top of the htaccess file in example1.com's document root.
In RewriteCond rule you use !, which mean negate the result of the condition.

301 redirect a whole directory to a specific thread/link (same domain)

I would like to add a 301 redirect to a whole directory and redirect every single link of this directory to a specific thread/post on the same domain.
I am using an apache server so i suppose i will have to edit the .htaccess
I want all the threads of this directory:
http://example.com/f73
To be redirected there:
http://example.com/f75/newthread-1990/#post333
I have read some different solutions and got confused :(
Some guys recommend to use the "Redirect" command and other recommend the "RedirectMatch" command...
I also read that the command should be different if the .htaccess is already edited and has some rewrite rules. Not sure if it makes sense or if i misunderstood, but at the moment i have added the following lines to the .htaccess:
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ htt://example.com/$1 [L,R=301]
You can use this rule in your root .htaccess:
RewriteEngine On
RewriteRule ^f73 /f75/newthread-1990/#post333 [L,NC,NE,R=301]

Limit access to subdirectory via specific SERVER_PORT using htaccess

I've been trying to limit access to a specific subdomain via the server port, e.g. it can only be accessed from subdomain.domain.com:8443 and no other ports.
I'm currently using hostgator for my webhost, and it's already been setup such that subdomain.domain.com points to the correct subdirectory.
Now in the htaccess file, I'm currently trying this:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /subdomain
RewriteCond %{SERVER_PORT} !^8443$
RewriteRule ^ - [F]
RewriteCond %{SERVER_PORT} ^8443$
RewriteRule ^(.*)$ http://%{HTTP_HOST}/subdomain/$1 [R=301,L]
As far as the blocking of other ports goes, it seems to work since accessing either subdomain.domain.com or www.domain.com/subdomain, I get a 403 forbidden page. But I can't get it to load the normal content correctly when I do access it via subdomain.domain.com:8443 or www.domain.com:8443/subdomain. Am I doing the rewrite conditions and rules correctly?
Thanks!
The R=301 tells the server to do a redirect, not a (silent) rewrite. So, when you load via 8443, the user is redirected to http://%{HTTP_HOST}/subdomain/$1 without the port 8443 specification and they are then blocked by the first rule. If you do a curl -I on subdomain.domain.com:8443 you should see the 301 redirect code rather than 200.
Remove the R=301 and remove the full domain specification in the final RewriteRule to leave:
RewriteRule ^(.*)$ subdomain/$1 [L]
This should do a silent rewrite of the content.

301 redirect with .htaccess - redirect if www is included or not

I've got the following 301 redirect in my .htaccess
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^old-site\.com,$ http://www.new-site.com/? [R=301,NE,NC,L]
If i visit
old-site.com
I am redirected correctly.
However, if I visit www.old-site.com, then it doesn't work.
Is there a way of effectively ignoring the www
Edit
There are several entries like this...
for example:
www.old-site.com/page-a-242.html to www.new-site.com/page-a
RewriteRule ignores the domain, so your first rule could be just
RewriteRule ^$ http://www.new-site.com/? [R,L]
For the other specific mappings you might use RewriteMap. See txt: Plain text maps for details on how to use it.
The drawback with RewriteMap is, that it can only be used in the main server config or in a virtual host environment.

htaccess with wildcard SSL

We have a Wildcard SSL Certificate that is supposed to work on any subdomain of a given domain.
So in this server we have this file structure:
/home/DOMAIN/public_html/subdomainx
/home/DOMAIN/public_html/subdomainy
etc...
Now, the Certificate is installed, but when you visit any subdomain over https (example: hxxps://subdomainx.domain.com ) it points to
/home/DOMAIN/public_html/index.php
We need that when you visit a subdomain via https
hxxps://subdomainx.domain.com
That it points to the the same directory that it's http equivalent:
/home/DOMAIN/public_html/subdomainx
Our provider tells us that this is not possible, that the current behaviour is correct, and that we should do some htaccess to achieve this.
I've tried a few things, incluiding this solution, that seems to be what I need: Advice on Configuring .HTaccess file to Redirect HTTP Subdomain to HTTPS Equivalent
But can't get it to work.
Any tips?
Thanks.
You don't want the [R] flag in your rewrite rule, or the hostname in the target. You want to rewrite the URI so that a "subdomainx" is prepended to it. Something along the lines of:
RewriteEngine On
# We don't want the main domain or www
RewriteCond %{HTTP_HOST} ^((?!www).*)\.domain.com$ [NC]
# Make sure that if we rewrite, the destination actually exists (to prevent loops and properly return 404's)
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/%1%{REQUEST_URI} -d
# rewrite
RewriteRule ^(.*)$ /%1/$1 [L]
Note that the %{DOCUMENT_ROOT}/%1%{REQUEST_URI} checks have to be made so that proper 404's get returned without information disclosure. This makes it so when you request for http://test.domain.com/badfile.html, the 404 message says "/badfile.html" is not found, instead of "/test/badfile.html" is not found.

Resources