mod_rewrite does not hide subdirectory - mod-rewrite

On my server I have multiple domains.
RewriteEngine On
RewriteBase /
# 1. Redirect multiple domains to http://domain.de/
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.de [NC]
RewriteRule ^/?(.*) http://domain.de/$1 [L,R,NE]
# 2. Redirect all requests to /subdirectory
RewriteRule ^$ /subdirectory [L]
The 2. rule is working correctly, but it is not hiding the subdirectory in the url nor does it work as intended: a request for http://domain.de/content/image.png returns 404, because the actual file is located in http://domain.de/subdirectory/content/image.png
Furthermore, I have some folders with tools located aside the subdirectory /subdirectory. I want to make sure I can still access them. This is currently working.
Question
How can I make sure, that the request for http://domain.de/content/image.png works?
What I tried
RewriteCond %{REQUEST_URI} !^/subdirectory/
RewriteRule (.*) /subdirectory/$1 [L]
But that just returns error 500 with the following entry in the apache error log: `Request exceeded the limit of 10 internal redirects due to probable configuration error.
Edit
After guidance provided by Ravi Thapliyal, there is (I guess) one thing remaining: Removing the subdirectory from the url.
[user#domain html]$ curl -I domain.de/
HTTP/1.1 301 Moved Permanently
Date: Mon, 21 Oct 2013 12:42:22 GMT
Server: Apache/2.2.22 (Ubuntu)
Location: http://domain.de/subdirectory/index.php
Vary: Accept-Encoding
Content-Type: text/html
This is what gets returned, but I actually want to get the html not a location header which then of course redirects me externally to the subdirectory, which is then visible to the user. Might it have something to do with another .htaccess file from a subdirectory?
Edit2
Seems the problem is related to the typo3 installation behind the subdirectory. The accepted answer works as expected.

Your first rule should do an external redirect (changing the domain internally won't matter at all)
RewriteCond %{HTTP_HOST} !^(www\.)?domain\.de [NC]
RewriteRule ^/?(.*)$ http://domain.de/$1 [R=301,L,NE]
Your second rule is not required. The new rule would cover root / request as well.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d [OR]
RewriteCond %{REQUEST_URI} ^/?$
RewriteCond %{REQUEST_URI} !^/subdirectory [NC]
RewriteRule ^(.*)$ /subdirectory/$1 [L]
The two RewriteConds on %{REQUEST_FILENAME} would make sure you can access any files -f or directories -d present outside /subdirectory.
Basically, the condition %{REQUEST_FILENAME} !-d prevents redirection if the URL path points to any existing directory. This prevents a URL like /existing-directory to be redirected to /subdirectory/existing-directory.
But, this could also prevent a root URL / request which is why you received a directory index forbidden error. Hence, the above condition is [OR]'d with %{REQUEST_URI} ^/?$ to allow / to be redirected to /subdirectory as well.

Related

Code igniter installation in subfolder throw CORS in XMLhttprequest

I have installed codeigniter application in a subfolder, whenever i send ajax request i get this error:
No 'Access-Control-Allow-Origin' header is present on the requested resource.
My .htaccess is:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
My config.php is:
$config['base_url'] = 'http://jmwglobal.com/locker/';
I don't know why i am getting this error. I have tried almost all remedies available on internet/stackoverflow but no solution found.
I find the solution, It was deeply written in somewhere in STACKOVERFLOW.
Thanks to this post.
No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '...' is therefore not allowed access
JavaScript code is limited by the same-origin policy, meaning, from a page at www.example.com, you can only make (AJAX) requests to services located at exactly the same domain, in that case, exactly www.example.com (not example.com - without the www - or whatever.example.com).
What I did is i just put these two lines in my htaccess,
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
These line actually remove www from URL. This is important because as #acdcjunior Said in the above post that www.domain.com is different from domain.com for javascript.
BIG THANKS TO #acdcjunior.

How do I strip out ?_escaped_fragment_= using .htaccess

Google discovered that I'm allowing end users to navigate my content using ajax loading, and is loading my pages as a user client rather than requesting them as new page loads. So instead of trying to index www.mysite.com/page, it's requesting www.mysite.com/?_escaped_fragment_=/page
Which is not at all what I want it to do. My snapshots are served at the same URL as the ajax-loaded content. The site is not using queries, it's not supporting them and I don't want to build that support. This means that all the pages look broken to google which of course is unfortunate!
Currently all page requests are redirected server side using .htaccess sending requests to the index.php file which in turn compiles the html doc on the server before serving to the client. The site serves perfectly valid and unique html documents for all pages. But google insists on doing it the ajax way and adding the query which always returns a broken page.
I'm not a .htaccess expert, but it seems to me that the easiest way to solve this would be to rewrite the request, remove the ?_escaped_fragment_=/ bit and permanently redirect any such requests to what currently works which is to load the pages using their correct url's.
Anyone know how I would go about doing that? Below is the current redirect part of my .htaccess file which needs to be amended with the _escaped_fragment_ stripping code:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
#if trailing / remove it with a permanent redirect
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
#if missing www. add it with a permanent redirect
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
#requests for index.php never rewritten
RewriteRule ^index\.php$ - [L]
#if file or directory are missing, route to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
This is how I rewrote it so that all ?_escaped_fragment_=/XXXXX requests got redirected to /XXXXX without the query
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=(.*)$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%1? [L,R=301]
This makes www.domain.com/?_escaped_fragment_=/somepage redirect (permanently) to www.domain.com/somepage
...which is just what I wanted.

.htaccess works in local but not on server (but no 404 error)

I have a .htaccess that is supposed to rewrite my URL. My host has told me that it supports URL rewriting, and I verified that by using phpinfo() and checking.
Anyways, this is my .htaccess:
RewriteEngine On
RewriteRule ^([_a-zA-Z0-9]+)$ index.php?page=$1 [R]
It works like a charm in local, but on my server, it doesn't do anything.
I checked this before on the internet and some people had it, but they all had a 404 error, while I don't have a 404 error. It simply doesn't redirect, it doesn't do anything, so I get all kind of error messages.
RewriteRule ^([_a-zA-Z0-9]+)$ index.php?page=$1 [R]
The regex in your rule doesn't match strings with slashes at any position. I am not sure that's acceptable and you don't give any request examples, but I don't think it is.
You may try this rule-set in one .htaccess file at root directory:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !index\.php [NC]
RewriteRule ^([^/]+)/?$ index.php?page=$1 [L]
For permanent redirection replace [L] with [R=301,L].
You can make sure that the file (!-f) or directory (!-d) that you're matching doesn't exist before the rewrite. That way you don't end up with a 500 loop with something like /index.php?page=index. Additionally the ^ character is matching the beginning of the string, so if your original test was in a subdirectory it would not rewrite since you weren't allowing slashes.
This should work for any instance, however it will ONLY make the page variable the last string in the URI.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ([_a-zA-Z0-9]+)$ /index.php?page=$1 [R,L]

URL Rewrite to redirect a old host to a new host

I know nothing about Url Rewrites, but we have recently changed domain names from oldcompanyname.co.uk to newcompanyname.co.uk.
We have been told that to get oldcompanyname.co.uk to not show up in search results anymore, we need to redirect the old url to the new one.
I have installed ISAPI_Rewrite 3 but have not been successful in getting the re-direction to work.
On our development servers I've tried the following code without success to redirect any request to http://tm-devtest2/tmintranet to http://tm-devtest2/tmintranet.
# Helicon ISAPI_Rewrite configuration file
# Version 3.1.0.94
RewriteEngine On
RewriteCond %{HTTP_HOST} ^.*internet*$
RewriteRule ^$ http://tm-devtest2/tmintranet/ [R,L]
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !newcompanyname.co.uk$ [NC]
RewriteRule ^(.*)$ http://www.newcompanyname.co.uk/$1 [L,R=301]
The RewriteCond is only needed in case the same instance will serve both domains.
L means "Last", i.e. stop rewriting
NC means "no case"
R means Redirect
301 means "Permanently Moved"

mod_rewrite - some requests being rewritten should produce a 404 but don't

I've been working on creating seo friendly urls for my site and have done this successfully with the following rules:
RewriteEngine on
RewriteBase /
#redirect non www to www
RewriteCond %{HTTP_HOST} ^example.co.uk [NC]
RewriteRule ^(.*)$ http://www.example.co.uk/$1 [L,R=301]
# Redirect any request with page=var to /var/ format
RewriteCond %{QUERY_STRING} ^page=(.+[^/])$ [NC]
RewriteRule ^index\.cfm$ http://%{HTTP_HOST}/%1/? [R=301,L,NC]
# If not an existing file or directory rewrite any request
# to page var.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+[^/])/$ index.cfm?page=$1 [QSA,L]
My problem is that because of the last rule (I think), any root dir request is made into a friendly url such as /pagethatdoesntpointanywhere/ whether it points anywhere or not. Now I'd be happy with a 404 but it's not doing that it's just displaying the homepage.
I've also tried adding a blanket 404 rule:
# 404 files that don't exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .+ /notfound.html [R=404,L,NC]
But that makes all of the friendly urls 404s as well.
Could someone explain where I am going wrong here please?
Use ErrorDocument 404 /notfound.cfm to display not found error page when Apache cannot find the file (replace notfound.cfm by your file).
In index.cfm, if value of page parameter is unknown (e.g. pagethatdoesntpointanywhere), display 404 error page (use the same/similar code as notfound.cfm). It is the right place to do considering your rewrite rules and the fact that you checking which page to display here anyway. Lots of products/frameworks work in similar fashion (for example: WordPress).
Use both #1 and #2. Number #2 will work for 1-folder deep URLs (e.g /meow/) while #1 will catch any other URLs (e.f. /meow/kitten/ or /meow/wuf/oink.css).

Resources