redirection of non-existing folder mod_rewrite - .htaccess

How can i redirect a certain url request like:
http://sample.org/www/images/animal.png
file structure:
rootpath/images/animal.png
rootpath/www/

Try putting this in the htaccess file in your document root:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/www/(.*)$
RewriteCond %{DOCUMENT_ROOT}/%1 -f
RewriteRule ^/?www/(.*)$ /$1 [L]

Related

Virtual Directory htaccess and url rewriting

I have a website in php which has some pages:
www.example.com/news
www.example.com/people
www.example.com/music
On each of these pages is an overview of content. If you click, for example, on a link on www.example.com/news, an article opens with that url using a template (www.example.com/article.php?url='example-of-article-url')
For each of these pages there is a rewrite needed. The displayed url should be www.example.com/news/example-of-article-url
and this should acces the article.php file. The news directory is virtual. The www.example.com/news, www.example.com/people, www.example.com/music are being rewritten by this code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
What I have been trying is:
RewriteRule ^news/(.*) article.php?url='$1' [R=301,L]
However, when this is loaded, css and images aren't. The content is generated thinking it's in the /news/ directory, thus unable to find the other files. I can't use canonical URL's because of SEO-reasons.
You should add:
<base href="/"/>
In your <head>section.
First of all dont forget to add this at the very start:
RewriteEngine On
Remove the Qoutes from '$1' your last line and try the method below instead.
RewriteEngine On
RewriteRule ^news/(.*)$ article.php?url=$1 [R=301,L]
To redirect with the contents of a whole directory to your location:
RewriteEngine On
RewriteRule ^news/(.*)$ article.php?url=$1 [R=301,NC,L]
Or you can do it this way too with a Slash on the Start:
Redirect the whole folder (news) to your new location:
RewriteEngine On
RewriteRule ^/?news/(.*)$ article.php?url=$1 [R=301,L]
or
RewriteEngine On
RewriteRule ^/news/(.*)$ article.php?url=$1 [R=301,L]
To redirect with the contents of a whole directory to your location:
RewriteEngine On
RewriteRule ^/news/(.*)$ article.php?url=$1 [R=301,NC,L]
Best of Luck

htaccess redirect subfolder

I would like to redirect the url http://intranet/trac/paradox/report/6 to http://cobra.woking/trac/paradox/report/6. trac is a subfolder and paradox is a subfolder. report/6 are params that need to be kept and may change.
In my apache doc root i have
#/opt/html/.htaccess
Redirect 301 / http://intranet/intranet
RewriteEngine On
RewriteRule ^trac/paradox/report/6$ http://cobra.woking/trac/paradox/report/6 [L,R]
I have tried the following which does not work
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} !^intranet$
RewriteRule (.*) http://cobra.woking/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^intranet$
RewriteRule (.*) http://cobra.woking/$1 [R=301,L]
URL i want to change is http://intranet/trac/paradox/ to http://cobra.woking/trac/paradox/. I have placed .htaccess in the /opt/html/trac/paradox/.htaccess
In the htaccess file of your intranet's document root, add this to the top of the file:
RewriteEngine On
RewriteRule ^trac/paradox/report/6$ http://cobra.woking/trac/paradox/report/6 [L,R]

How to redirect to a subfolder with htaccess

How I can redirect to a specific subfolder with htaccess?
I have a structure like this
dfg/pubblication1/index.html
In the folder directory there is no index.html file but only the .htaccess file
What I want to do is that when a user is linked to http://domain.ext/folder will be automatically redirect to http://domain.ext/folder/subfolder/index.html
I have something like this
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/dfg [NC]
RewriteRule ^(.+) /dfg/pubblication1/$1 [NC]
</IfModule>
But unfortunately doesn't work.
Thanks in advance
This is what I use:
RewriteCond %{REQUEST_URI} !^/static/
RewriteCond %{DOCUMENT_ROOT}/static%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/static%{REQUEST_URI} -d
RewriteRule ^(.*)$ /static/$1 [L]
Line 1: Check if the request already contains the subfolder you want to redirect (to avoid an infinite loop).
Line 2 & 3: Verify if the request is a file or directory
Line 4: Do the actual rewriting

Add A 301 Redirect to Codeigniter .htaccess

I've been trying to set up a redirect for a page that recently moved. The page was originally at http://example.com/foo/, but has since moved to http://example.com/foo/bar/ .
I tried the following rule in my sites .htaccess file:
RedirectMatch 301 ^/foo/$ /foo/bar/
However going to the url http://example.com/foo/ resulted in a redirect to the url http://example.com/foo/bar/?/foo/. While the url works and the page I want to redirect to loads, I would quite like to get rid of the extra ?/foo/ at the end of the url.
Here is my full .htaccess:
RewriteEngine On
RewriteBase /
# add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [R=301,L]
# allow access to certain directories in webroot
RewriteCond $1 !^(index\.php|robots\.txt|css/|lib/|js/|images/|^(.*)/images)
# gets rid of index.php
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# page redirects
RedirectMatch 301 ^/foo/$ /foo/bar/
Adding a RewriteRrule to the top of the .htaccess after the RewriteBase / file solved the problem.
RewriteRule ^foo/$ /foo/bar [R=301,L]
I found it easier to redirect from controller instead of .htaccess because .htaccess was adding a querystring at the end.
For example I've put this in my controller's action:
if ($this->uri->segment(2)==='old_url') {
redirect(base_url() . $this->lang->lang() .'/new-url', 'location', 301);
}

htaccess and rewriting urls

currently i have a /en/ folder that is empty except for a .htaccess with the following
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)$ ../index.php?language=en$1 [NC]
i use it to eliminate the need for
index.php?language=en
in all my URLs. I would like to modify the htaccess in a way that i no longer need the /en/ folder with nothing but the htaccess inside. ideally i would like an htaccess in my root folder that reads the url and if it is www.example.com/en/ to rewrite to www.example.com/index.php?language=en
This should work for you:
RewriteRule ^en/(.*)$ index.php?language=en$1 [NC]
Put the following code in .htaccess file in your root folder.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^en/(.*)$ index.php?language=en$1 [L]
Here $1 will append rest of the url as well. The condition will also help if you request your files using direct url.

Resources