I created some static sub-domains for images:
www.static1.domain.com
www.static2.domain.com
Now I want to redirect files that are not images from static domains to www.domain.com, to avoid duplicate content. I have these rules in my htaccess (non-existing files are redirected to index.php silently):
#Redirect static to main
RewriteCond %{HTTP_HOST} static([0-9]+)\.
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} !\.(js|css|png|jpg|jpeg|gif)$ [NC]
RewriteRule (.*) http://www.domain.com%{REQUEST_URI} [R=301,L]
#Redirect non exisitng files to index.php (silent)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
The redirect works fine. But if I enter a non-existing image e.g. http://www.static1.domain.com/test.gif, I am redirectd to http://www.domain.com/index.php.
The redirect of test.gif should be a silent redirect to index php ... what I am doing wrong?
Thanks for hints.
I'm not sure if I'm understanding you correctly. By silent redirect to you mean that "index.php" shouldn't be in the url bar or do you mean the url bar should still read "test.gif" but the page should render index.php?
You can not add multiple RewriteCond lines. Only the last one applies to RewriteRule.
So this line does not have any effect:
RewriteCond %{REQUEST_FILENAME} -f
And that is why non existing images are also being redirected.
How about:
#Redirect non exisitng files to index.php (silent)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /index.php [L]
#Redirect static to main
RewriteCond %{REQUEST_FILENAME} !\.(js|css|png|jpg|jpeg|gif)$ [NC]
RewriteRule (.*) http://www.domain.com%{REQUEST_URI} [R=301,L]
Did it this way. Throws the default server 404 Page when http://www.static1.domain.com/test.gif is not found. Not best, but well.
RewriteCond %{HTTP_HOST} !static([0-9]+)\.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} static([0-9]+)\.
RewriteCond %{REQUEST_FILENAME} !\.(js|css|png|jpg|jpeg|gif)$ [NC]
RewriteRule (.*) http://www.domain.com%{REQUEST_URI} [R=301,L]
Related
I'm trying to use my .htaccess file to point domains/subdomains to directories/subdirectories without changing the url in the browser.
Examples of the incoming url and the directory it should point to:
domain1.com/* -> /domain1.com/www/*
foo.domain1.com/* -> /domain1.com/foo/*
bar.domain1.com/* -> /domain1.com/bar/*
domain2.com/* -> /domain2.com/www/*
foo.domain2.com/* -> /domain2.com/foo/*
bar.domain2.com/* -> /domain2.com/bar/*
Here's my current attempt that is continually appending the directory/subdirectory to the url:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(?:([^.]+)\.)?([^.]+)\.([^.]+)$ [NC]
RewriteCond %{REQUEST_URI} !^/%2\.%3/%1/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /%2.%3/%1/$1 [L,NE,P,QSA,R]
Why so complicated? The following should satisfy all requirements you listed:
RewriteCond %{HTTP_HOST} ^([^.]+)\.[^.]+$
RewriteRule ^ /www%{REQUEST_URI} [L]
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)\.[^.]+$
RewriteRule ^ /%1%{REQUEST_URI} [L]
Have a try yourself at made with love
It took a little work, but that to the help of the first response, I was able to get everything working eventually.
Here's my final solution to this issue:
## Turn the rewrite engine on to allow for url mapping to work
RewriteEngine on
## Don't require a trailing "/" for directories
DirectorySlash Off
## If subdomain was missing, redirect into the "www" subdirectory
# If no %{REQUEST_FILENAME} was provided, redirect to the "www" subdirectory
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)$ [NC]
RewriteRule ^$ /%2.%3/www/ [END,NC]
# If a %{REQUEST_FILENAME} was provided, redirect to the directory/file in the "www" subdirectory
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /%2.%3/www/$1 [END,NC,QSA]
# If subdomain was given, redirect into the given subdirectory
# If no %{REQUEST_FILENAME} was provided, redirect to the given subdirectory
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)\.([^.]+)$ [NC]
RewriteRule ^$ /%2.%3/%1/ [END,NC]
# If a %{REQUEST_FILENAME} was provided, redirect to the directory/file in the given subdirectory
RewriteCond %{HTTP_HOST} ^([^.]+)\.([^.]+)\.([^.]+)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /%2.%3/%1/$1 [END,NC,QSA]
I'm trying to redirect users from www.hostname.com/map/ to www.hostname.com/map.php if /map/ does not exists. I have the following .htaccess file already:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Which does the following:
Redirect users automatically to the https:// variant of my website
Redirects /filename to /filename.php if there is no map called /filename/ without changing the visible url
But now I want to redirect users from /map/ to a file called /map.php as well if there is no map called /map/. Ofcourse I don't want to display the .php portion of the file name, so I basically want users to be redirected to /filename if the map /filename/ does not exists without changing the visible url. How can I rewrite the code above to make this possible?
You can use:
RewriteEngine On
RewriteBase /
# keep redirect rule before internal routing one
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,NE,R=301]
# if matching .php file exists forward it
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
I manged to remove the folder from the url but I cannot redirect back to it. Please help!
# externally redirect /dir/foo to /foo
RewriteCond %{THE_REQUEST} pages/
RewriteRule pages/(.*)$ $1 [L,NC,R]
# internally forward /foo to /dir/foo
#what goes here?
EDIT:
Also, how can I put this on the same code?
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ profile.php?username=$1 [L]
I want the user name to be like this localhost/test/user1 rather than localhost/test/profile.php?username=user1
Internal redirect mean URL rewriting (if I'm not misunderstood)
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/dir/
RewriteRule ^(.*)$ /dir/$1 [L]
Note: To prevent external redirect I didn't used [R] flag.
EDIT
I can see you hostead on localhost in subdirectory /test So, you'l have to set RewriteBase as following:
RewriteEngine On
RewriteBase /test
RewriteCond %{REQUEST_URI} !^/dir/
RewriteRule ^(.*)$ /test/dir/$1 [L]
EDIT Included user profile rewrite rule
RewriteEngine On
RewriteBase /test
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /test/profile.php?username=$1 [L]
RewriteCond %{REQUEST_URI} !^/dir/
RewriteRule ^(.*)$ /test/dir/$1 [L]
I'm moving my site from the .com version to the .co.uk. The only issue is there are some admin facilities that I want to keep on the .com site. I've set up my .htaccess to redirect everything but requested files and directories that exist which works fine, except for the root of the site, I can't get that to redirect.
Here is my htaccess...
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ http://www.site.co.uk/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) http://www.site.co.uk/$1 [R=301,L]
You can use this code:
RewriteEngine On
RewriteBase /
RewriteRule ^(index\.php)?$ http://www.site.co.uk/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) http://www.site.co.uk/$1 [R=301,L]
This regex ^(index\.php)?$ will match index.php OR empty string (landing page).
I am working on a twitter/facebook type site for a college class. Somehow they let a professor teach this class with no PHP, CSS, HTML, JavaScript,jQuery, or Ajax knowledge. I have been trying to rewrite my URLs to make them look like twitter. I have gotten all of my user profile pages to rewrite to: www.site.com/username from: www.site.com/profile.php?name=username. However, I also want to rewrite my login page, create account page, etc. Currently they are: www.site.com/login.html , www.site.com/createAccount.html. I want the to rewrite without the html. Here is my .htaccess file currently.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?name=$1 [L]
**Update
I apologize I ended up switching all of my files over to .php. But I am still having some issues. Now, I only want to redirect specific URLs. For example: www.361orc.info/login should internally redirect to www.361.orc.info/login.php . I cannot seem to figure out what is wrong with the following code. It redirects but it does it changes the client URL. I want it to just redirect internally. Here is my .htaccess file:
RewriteEngine On
#I want this code to change .com/login.php to .com/login but only internally
#the URL in the client's browser shouldn't change
RewriteCond %{REQUEST_FILENAME} !-
RewriteRule ^login?$ login.php [L]
#Change the profile pages of users from .com/profile.php?name=user to .com/user
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ /profile.php?name=$1 [L,QSA]
you could do something like this
# Rewrite User Profiles
RewriteCond %{QUERY_STRING} ^name=([^&]+) [NC]
RewriteRule ^profile.php$ /%1 [R=301,L]
# Rewrite Login
RewriteRule ^login.html$ /login [R=301,L]
# Rewrite Create Account
RewriteRule ^createAccount.html$ /createAccount [R=301,L]
You are pretty close, just an external redirection rule that will redirect .html files to ones without .html extension:
RewriteEngine On
RewriteBase /
# To externally redirect /dir/file.html to /dir/file
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ %1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^\.]+?)/?$ $1.html [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)$ profile.php?name=$1 [L,QSA]