mod_rewrite prefix as a folder - apache

I have this structure in my home directory:
/index.php
/.htaccess
/folderx/
/folderx/subfolder/
/folderx/subfolder/file.php
What I need, is to mod_rewite all requests with the prefix "folderx_" to the real folderx directory. But additionally, I need to force redirection - the user should't be available to request /folderx/subfolder/subsubfolder/file.php directly. It should be redirected to the mod_rewrite url: /folderx_subfolder/subsubfolder/file.php
/folderx_subfolder/ => /folderx/subfolder/
/folderx_subfolder/file.php =>
/folderx/subfolder/file.php
/folderx_subfolder/subsubfolder/ =>
/folderx/subfolder/subsubfolder/
/folderx_subfolder/subsubfolder/file.php
=> /folderx/subfolder/subsubfolder/file.php
I've tried the code below, but the redirect is not working properly. When requesting the subfolder, it redirects to the prefix, but I don't know how to prevent looping( Please, help!
/folderx/subfolder/subsubfolder/file.php
RewriteCond %{REQUEST_URI} ^/folderx/(.*)$
RedirectMatch ^/folderx/(.*)$ /folderx_$1
RewriteRule ^folderx_([a-z]{3,15})(/?)+$ /folderx/$1/ [NC]
RewriteRule ^folderx_([a-z]{3,15})/(.*)$ /folderx/$1/$2 [NC]

RewriteRule ^folderx/(.+)$ http://example.com/folder_$1 [nc,last,redirect]
RewriteRule ^folderx_(.+)$ /folderx/$1 [nc]

Related

url rewriting to virtual path keeping always the virtual path

I have a physical folder:
http://www.myowndomain.com/path1/
and I want that all requests to http://www.myowndomain.com/path1/* and http://www.myowndomain.com/path2/* (that is a virtual folder, it doesn't exists) will be redirected to http://www.myowndomain.com/path1/*. Moreover I want all those requests show in the url bar
http://www.myowndomain.com/path2/*
Where should I put .htaccess file and what would it be its content?
Add the following rule to you /root.htaccess
RewriteEngine on
#1)externally Redirect /path1/* => /path2/*##
RewriteCond %{THE_REQUEST} /path1/(.*)\sHTTP [NC]
RewriteRule ^ /path2/%1 [NE,L,R]
#2)internally Redirect /path2/* => /path1/*##
RewriteRule ^/?path2/(.*)$ /path1/$1 [NC,L]
This will internally redirect /path2/ to /path1 .

Redirect all pages in direcory except one subdirectory .htaccess

I want to have one URL still being served from a subdirectory on the old domain but redirect all others from the parent directory to the new domain.
www.olddomain.co.uk/directorya/directoryb -> no redirect
www.olddomain.co.uk/directorya -> www.newdomain.co.uk/directorya
This works if the URL is typed with a trailing slash
RewriteCond %{REQUEST_URI} !^/directorya\/directoryb [NC]
RewriteRule ^(.*)$ http://www.newdomain.co.uk/ [L,R=301]
So http://www.olddomain.co.uk/directorya/directoryb/ works but http://www.olddomain.co.uk/directorya/directoryb fails.
Tried added $ at the end and other connotations and tried
RewriteEngine on
RewriteRule !^myspecialdirectory/mynextdirectory($|/) http://newdomain.example%{REQUEST_URI} [L,R=301]
from Redirect entire site except one directory to a new site - apache .htaccess
What do I need to do to get http://www.olddomain.co.uk/directorya/directoryb/ or http://www.olddomain.co.uk/directorya/directoryb to redirect? (while all other http://www.olddomain.co.uk pages go to http://www.newdomain.co.uk
For reasons I don't understand http://www.olddomain.co.uk/directorya/directoryb goes to Google and doesn't redirect
Try using THE_REQUEST variable and make sure this this very first rule:
RewriteCond %{THE_REQUEST} !\s+/+directorya/directoryb [NC]
RewriteRule ^ http://www.newdomain.co.uk/? [L,R=301]
Test it after clearing your browser cache.

Apache htaccess sending URLs in a URL

I'm trying to send a URL in a URL, and there's ModRewrite in the middle.
Here's the Rewrite Rule. It's used to search for a user's profile.
RewriteEngine On
RewriteRule ^(query)/(profile)/([a-z0-9:.()\-\ ]+)/?$ search/index.php?scope=$2&query=$3 [NC,L]
And this is the URL
http://www.example.com/query/profile/http://www.facebook.com/example
No matter how I do the rewrite rule, I keep getting NetworkError: 500 Internal Server Error or NetworkError: 404 Not Found if it's a URL I put there.
How can I do this right? I'm on Windows 7 Home Premium. Am I missing something?
The URL above is passed using ajax.
Edit:
#htaccess in site1 folder
RewriteRule ^resources/?(.*)/?$ /control/$1 [NC,L]
#htaccess in control folder
RewriteRule ^(query)/(profile)/([a-z0-9:.()\-\ ]+)/?$ search/index.php?scope=$2&query=$3 [NC,L]
next folder is `search` which has `index.php`
URL used
http://www.example.com/resources/query/profile/http://www.facebook.com/example
EDIT:
I redid the RewriteRule like below, and don't get any error messages now, but Apache seems to be taking off one / from the url after the http://. So I get only http:/www.facebook.com/example. Any help with that will be appreciated.
RewriteRule ^(query)/(profile)/([a-z0-9\/:.()\-\ ]+)/?$ search/index.php?scope=$2&query=$3 [NC,L]`
You need to capture URL from RewriteCond otherwise Apache will trim multiple // into a single one.
Change this rule in root .htaccess:
RewriteCond %{REQUEST_URI} /resources/(.+)$ [NC]
RewriteRule ^ /control/%1 [L]
Have this in /control/.htaccess file:
RewriteEngine On
RewriteBase /control/
RewriteCond %{REQUEST_URI} /(query)/(profile)/(.+)$ [NC]
RewriteRule ^ /resources/search/index.php?scope=%2&query=%3 [QSA,L]

301 Redirect for all pages to new site Except 1 directory

Im trying to create a 301 redirect for all pages but one directory to a new site, but Im having trouble setting it up.
This is basically what I need:
http://www.example.com/store => no redirects, users remain on http://www.example.com/store
http://www.example.com/* => all other pages go to this url http://www.newdomain.com/
AKA
http://www.example.com/apple => http://www.newdomain.com/
http://www.example.com/pie => http://www.newdomain.com/
http://www.example.com/foo/bar => http://www.newdomain.com/
I tried this method:
RewriteEngine on
RewriteCond %{REQUEST_URI}!^/store/
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
But when I go to http://www.example.com/store it takes me to http://www.newdomain.com/store
Basically I need the directory /store to remain on the old domain. Can anyone help? Not to experienced with .htaccess rules...
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/store
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]

mod_rewrite condition for existing folder

I'm trying to set-up an .htaccess file that will pass every request URL as GET into a file called index.php. The only exception is, when the request URL points to a directory res.
Examples:
/wiki/cool-stuff => index.php?uri=/wiki/cool-stuff
/wiki/cool-stuff?news=on => index.php?uri=/wiki/cool-stuff&news=on
/res/cool-photo.jpg => res/cool-photo.jpg
Two problems:
The GET variable passed to /wiki/cool-stuff in the second example is not passed to index.php
Accessing /res (not /res/!!) suddenly shows me /res/?uri=res in my browser and index.php with uri=res/. Accessing /res/ instead, shows me index.php with uri=res/ and the URL in the browser stays (which is okay).
The .htaccess:
RewriteEngine on
RewriteBase /subthing/
RewriteCond %{REQUEST_URI} !/res/(.+)$
RewriteCond %{REQUEST_URI} !index.php
RewriteRule (.*) index.php?uri=$1
How can I achieve the desired behaviour?
Try using the Query-String-Append flag, QSA
Make the trailing slash optional - in Regex, this is achieved by adding ?.
New .htaccess:
RewriteEngine on
RewriteBase /subthing/
RewriteCond %{REQUEST_URI} !/res(/.*)?$
RewriteCond %{REQUEST_URI} !index.php
RewriteRule (.*) index.php?uri=$1 [QSA]
Note that I have tweaked the Regex on the /res folder to cause /resabc to be redirected (if the slash was the only optional piece, anything beginning with res would match.
Apache Mod_Rewrite Documentation