mod_rewrite for 2 same files in different folders - .htaccess

I have files:
search_result.php
new/search_result.php
mod_rewrite URL for these files are
www.domain.com/search?q=
www.domain.com/new/search?q=
The first one is working fine. But when I open www.domain.com/new/search?q=, its picking the file search_result.php in the root folder, I want it to pick the file new/search_result.php
The mod_rewrite code I'm using for both the folders is:
RewriteRule ^search$ search_result.php [QSA,L]
Thanks in advance!

Your rewrite rule should be like this:
RewriteRule ^([^/]+/)?search/?$ /$1search_result.php [QSA,L,NC]

Related

Rewrite multiple sub-directories to the same path htaccess

I want to rewrite multiple sub-directories to the same path. while I can remove a part from URL Apache htaccess
for example, I want
domain.com/category/computer/internet-topics
domain.com/category/computer/windows-topics
domain.com/category/science/physics-topics
domain.com/category/science/biology-topics
to be
domain.com/category/internet-topics
domain.com/category/windows-topics
domain.com/category/physics-topics
domain.com/category/biology-topics
When I use this, it works fine for the first line only (computer)
RewriteEngine On
RewriteRule ^category/([a-zA-Z0-9_-]+)$ category/computer/$1
RewriteRule ^category/([a-zA-Z0-9_-]+)$ category/science/$1
I think you need to be specific else only first rule will be used
RewriteEngine On
RewriteRule ^category/((internet)|(windows)-topics)$ category/computer/$1
RewriteRule ^category/((physics)|(biology)-topics)$ category/science/$1

Use .htaccess to load files from different folders

I have many files in directory /full/
that's why I would like to spread files to /full1/, /full2/, /full3/ folders on server but to save original URL like
http://my-domain.com/full/article-with-text
to determine which files are where to put I'd like to define it on the URL mask like
^/full/a$ from folder /full1/
^/full/b$ from folder /full2/
tell me please how to build correct .htaccess ?
Try this
RewriteRule ^full/a(.+)?/?$ http://my-domain.com/full1/a$1 [NC,L]
RewriteRule ^full/b(.+)?/?$ http://my-domain.com/full2/b$1 [NC,L]
RewriteRule ^full/c(.+)?/?$ http://my-domain.com/full3/c$1 [NC,L]
...
RewriteRule ^full/z(.+)?/?$ http://my-domain.com/full26/z$1 [NC,L]
e.g,
http://my-domain.com/full/a-one.txt
will mask the url
http://my-domain.com/full1/a-one.txt

.htaccess rewrite complete path w/ all files (no redirect)

I want to rewrite a directory to another directory. The file/link structure to all files (like css/js/images/GET vars etc.) have to keep intact.
My files currently are in http://www.example.org/_directory1/output/index.php?site=site1
The new path should be http://www.example.org/newDir/index.php?site=site1
In a nutshell /_directory1/output/ should changed to /newDir/.
RewriteRule http://www.example.org/_directory1/output/$1 ^newDir/(.*)$ [NC]
You would use
RewriteEngine On
RewriteBase /newDir/
RewriteRule ^(.*)$ http://www.example.org/newDir/$1
in your .htaccess in /_directory1/output/
The mod_rewrite documentation may help you further.

.htaccess redirecting requests to the same folder

I want to use .htaccessto redirect different requests to the same folder.
E.g.:
domain.de/ordner1/fileX.html
domain.de/en/folder1/fileX.html
domain.de/it/casella1/fileX.html
So whenever something is requested out of /ordner1/, /folder1/ or /casella1/ I want .htaccess to fetch the requested file out of a specific directory like domain.de/all/fileX.html.
I want to prevent duplicate content but also keep the foldernames in the selected language.
Could you help me solve this problem?
Try adding the following to the .htaccess file in the root directory of your site.
RewriteEngine on
RewriteBase /
#skip css, js etc
RewriteCond %{REQUEST_URI} !\.(css|js)[NC]
#if request to ordner or folder1 or casella1, serve the file from all/
RewriteRule ^(ordner1|en/folder1|it/casella1)/(.+)$ all/$2 [L,NC]
In your docroot/.htaccess:
RewriteEngine On
RewriteBase /
RewriteRule ^(ordner1/|en/folder1/|it/casella1/)(.*$) all/$2 [L]
You would need to add extra names to map other translation equivalents.

.htaccess redirect for images from old folder to new folder

I have just moved from Drupal + Wordpress to a site completely built in WordPress.
Duly I have a set of images where the files no longer exist and need to try and keep all the images in the one folder (if possible). Duly I need to send requests for any gif|png|jpg that are for http://www.domain.com/blog/wp-content/uploads/ to http://www.domain.com/wp-content/uploads.
If anyone could help would be appreciated - my .htaccess aint what it once was. Thanks in advance
If you google for "htaccess redirect", the top link is this:
http://www.htaccessredirect.net/
If you use the "301 Redirect Directory" section, you get this code:
//301 Redirect Entire Directory
RedirectMatch 301 /blog/wp-content/uploads/(.*) /wp-content/uploads/$1
As far as I know the target domain should be absolute, so the following might work:
//301 Redirect Entire Directory
RedirectMatch 301 /blog/wp-content/uploads/(.*) http://www.domain.com/wp-content/uploads/$1
Please try this rule in your .htaccess:
RewriteEngine On
RewriteRule ^/blog/wp-content/uploads/(.+)\.(png|gif|jpg)$ wp-content/uploads/$1.$2 [QSA,L]
Try this
RewriteEngine on
RewriteCond %{HTTP_REFERER} ^http://www.domain.com/blog/wp-content/uploads [NC]
RewriteRule .* http://www.domain.com/wp-content/uploads [NC,L]
You could try and put this
RewriteEngine ON
RewriteRule ^/blog/wp-content/(.*)$ http://newdomain.com/wp-content/$1 [R=301,L]
What I have hated about all the re-write rules and redirect options for .htaccess files is they all rely on hardcoding the path (URI) and/or server for the redirect.
The point of the ".htaccess" files it it should be for the current directory! It could be referenced in a number of different ways, installed on different servers in different locations. So trying it down to a specific location for a simple directory rename is illogical.
The solution is to somehow incorporate the current URI (regardless or where the ".htaccess" location) into the result...
This is my current solution for location independent ".htaccess" redirect for a renamed sub-directory, and even I admit it is not perfect... BUT IT WORKS...
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^OLDdir/.*$ %{REQUEST_URI}::: [C]
RewriteRule ^(.*)/OLDdir/(.*)::: $1/NEWdir/$2 [R,L]

Resources