.htaccess redirect match to subfolder - apache

I have the present structure now:
www.site.com/reportddmmyyyy
ex:
www.site.com/report01012015
www.site.com/report05012015
and I would like to move all the reports directory into a main directory: reports
www.site.com/reports/reportddmmyyyy
ex:
www.site.com/reports/report01012015
www.site.com/reports/report05012015
Many users have the old format in their bookmarks, so I need htaccess to detect that someone tried to access www.site.com/reportddmmyyyy
and show the content of reports/reportddmmyyyy without changing the browser URL.
Is it possible? I have been Googling for a long time but maybe I am not searching correctly. Could someone please point me in the right way?

RewriteEngine On
RewriteRule ^report([^/]+)/?$ /reports/report$1 [NC,L]
This rule will rewrite
/report01011990
to
/reports/report01011990
without changing the url in your browser.

Related

Using .htaccess to make subdirectory act like subdomain

It's a little difficult for me to ask the question correctly in title, but mainly here's what I'm trying to figure out.
If I use .htaccess on my website and say I'm trying to create a specific path "redirect" so that anything inside that path uses it's non-redirected path for it's relative path. Sorry for the structure of that sentence.
So for example...
I have my website as-
http://www.example.com/
I want to keep things organized so I create a sub-folder/subdirectory to keep all these folders inside of.
http://www.example.com/projects/
So the folder projects will be the place holder for all future projects and I want to create a folder inside projects for each project, so that I can define a URL for each one like such...
http://www.example.com/SuperFish/
http://www.example.com/AquaFear/
Now the folder for SuperFish and AquaFear would be under http://www.example.com/projects/ but also in their own folder say...
http://www.example.com/projects/01/
http://www.example.com/projects/02/
Now it'd be fine and dandy to keep the links like such, but for memory purposes and to share with other people the links to those projects, I'd like to just create a nice simpler URL with a custom "name". like http://www.example.com/SuperFish/ instead of http://www.example.com/projects/01/.
Now I was able to do this partially with my .htaccess file in the root folder with this code...
RewriteEngine On
Options +FollowSymLinks
RewriteRule ^SuperFish/?$ projects/01/ [NC,L]
RewriteRule ^AquaFear/?$ projects/02/ [NC,L]
The issue with this though is that now I can't use relative paths with my SuperFish or AquaFear made up URL links. Anything such as .css, .js, .etc... Isn't within scope because it's trying to look for those files through the made up URL rather than the real physical one.
So the question is, how would I make it so that I can use my made up URL and also have the webpage load things to the real physical path or somehow fix the relative paths by doing this within the .htaccess file?
The reason I titled this the way it is, is due to subdomains being able to find their files with relative paths even though they're a subdirectory just the same.
Using final $ blocks files inside this two folders from being redirect correctly. You could tried instead :
RewriteEngine On
Options +FollowSymLinks
RewriteRule ^SuperFish/?(.*)$ projects/01/$1 [NC,L]
RewriteRule ^AquaFear/?(.*)$ projects/02/$1 [NC,L]

simple mod_rewrite q...is the solution simple too?

I've looked at many examples here and all over the internet, but I can't seem to find an answer I understand, or that accurately solves my problem. I'm looking to implement a mod_rewrite directive in an .htaccess file that renames a folder to another name but does not show the name in the url bar.
For example (the user clicks a link that directs them to):
theSite.com/folder1/folder2/folder3/
I want them to see (same as above)
theSite.com/folder1/folder2/folder3/
But I want the browser to silently function in this directory
theSite.com/folder1/some_other_folder/folder3/
I am a PHP developer, writing my first web application. I can configure apache, PHP, mysql and use them like a pro. I'm sorry, but I don't understand the syntax for mod_rewrite. I can't seem to grasp it despite looking at many tutorials as I would need to ask questions before I could move onto the next concept. Thank you for your patience.
Your case is pretty run-of-the-mill. You just need to match the static string, plus a (.*) to match everything that follows it and store it into $1, then substitue some_other_folder.
The [L] flag (and absence of the [R] flag) instructs Apache to rewrite internally without redirecting the browser, and to stop here without matching further rules.
RewriteEngine On
RewriteRule ^folder1/folder2/folder3(.*)$ folder1/some_other_folder/folder3$1 [L]
If folder3 itself is part of the "dynamic" portion, that is, anything after folder2 should be silently rewritten into some_other_folder, leave folder3 out of the rule and just capture everything that follows folder2 into $1.
RewriteEngine On
RewriteRule ^folder1/folder2/(.*)$ folder1/some_other_folder/$1 [L]
I would use following
RewriteRule /folder1/folder2/folder3/ /folder1/some_other_folder/folder3/ [L]

Htaccess rewrite rules subfolder

i'm quite new in the world of Url Rewriting. I have to do so into our own CMS to have a good looking url and to get a better structure that gives results with search engine (SEO).
This is the background file that do all the job that we now hide with nice URL's:
http://something.com/fr/index.php
This is an example of my structure pattern :
http://something.com/fr/accueil or ...
http://something.com/fr/produits/solives-de-rive-rimboard
The CMS simply create an HTACCESS file into the FR directory for quite basic rewriterule like the following :
Options +FollowSymlinks
RewriteEngine On
RewriteRule produits/solives-de-rive-rimboard index.php?p=144
RewriteRule produits/decoupe-sur-mesure index.php?p=145
RewriteRule produits/panneaux-osb index.php?p=146
RewriteRule produits/boites-de-bois-crates index.php?p=147
RewriteRule produits/palettes-de-bois index.php?p=148
RewriteRule accueil index.php?p=4
RewriteRule a-propos-de-cimdat index.php?p=139
RewriteRule produits index.php?p=140
RewriteRule videos index.php?p=141
RewriteRule nous-joindre index.php?p=142
All works fine for first level page like "accueil", "nous-joindre"...
but I haven't found the work around for page of second and third level like "produits/solives-de-rive-rimboard". The index.php just load fine except that all relative link to this page are a subfolder away (the css, the image, jquery...).
Is there any tag that I can use into the htaccess file to specify that no matter in which level "accueil", "produits/something", or a third level one like "produits/group/something" for the index.php have a basic path ?
I'll prefer looking a work around within the htaccess instead of giving index.php a "<base href="something">" that might give us other problems in our structure.
Thanks
This is an HTML issue, rather than a server or .htaccess problem. If your browser sees something like /category/product it is going to act as if it is in the /category/ folder regardless of where the internal server side redirects are sending the request.
The fix to this is simple, change all of the linking in your html to be relative to the site root. So if you have an image tag like
<img src="img/button.gif" />
change it to
<img src="/img/button.gif" />
This tells the browser the exact path from the root to request files from regardless of your rewrite rules. This needs to be done for all of your relative links, including css and javascripts. It may be a bit of work to do, but once it is done it should make future maintenance much easier since you won't have to worry about the apparent path to the page.
Yes, this could be done via .htaccess fairly simply, but there are side effects, including the fact that search engines could see it as duplicate content and it could increase your server bandwidth use as users can't cache static content efficiently. Your best bet is to follow the best practice now while the site is new and growing than try to fix it later after a workaround has caused problems.

Apache: How to rewrite some dynamic URLs

I need to rewrite some specific URL's on my website but cannot find out how to do it despite searching for quite some time.
The original url that needs to be matched is in this format:
http://www.example.com/gallery/?level=picture&id=49
and I need them to be in this format:
http://www.example.com/index.php?page=gallery&level=picture&id=49
However, the folder also contains some image files as well. I need any URL's pointing directly to images to be left alone, and any URL's pointing to a php page to be rewritten as above.
I know what I want to do, but not how to do it. Basically I need to do this in .htaccess:
if(url contains 'gallery/' AND filetype != bmp/jpg/png/etc){
REPLACE '/gallery/' WITH '/index.php?page=gallery&' AND append original query string variables
}
Any help at all would be greatly appreciated.
So I have found a solution that has taken care of this problem for me. I have this working in my .htaccess file now.
RewriteCond %{REQUEST_URI} !\.(bmp|jpg|jpeg|png|gif)$
RewriteRule ^gallery/$ /index.php?page=gallery [R=302,QSA]
The first line (from what I have been told) excludes the file types listed from being affected by this rule, because as I originally mentioned I did not want the URL rewritten for images.
The second line takes a url like this:
http://www.yoursite.com/gallery/?level=picture&id=52
and turns it in to this:
http://www.yoursite.com/index.php?page=gallery&level=picture&id=52
and it leaves the original query string in place, in addition to the new "page=gallery" variable. It also does a 302 redirect so that the user is shown the correct address in their browser.
Not sure if this is helpful to anyone, but figured that since I posted asking about it, that I would post the solution I found as well.
This may a bit more general than you want, but it'll also avoid rewriting other existing files.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^/gallery/(.+)$ index.php [QSA]

Creating rewrite rules for multiple urls in the same folder

I have been asked by our client to convert a site we created into SEO friendly url format. I've managed to crack a small way into this, but have hit a problem with having the same urls in the same folder.
I am trying to rewrite the following urls,
/review/index.php?cid=intercasino
/review/submit.php?cid=intercasino
/review/index.php?cid=intercasino&page=2#reviews
I would like to get them to,
/review/intercasino
/submit-review/intercasino
/review/intercasino/2#reviews
I've almost got it working using the following rule,
RewriteRule (submit-review)/(.*)$ review/submit.php?cid=$2 [L]
RewriteRule (^review)/(.*) review/index.php?cid=$2
The problem, you may already see, is that /submit-review rewrites to /review, which in turn gets rewritten to index.php, thus my review submission page is lost in place of my index page. I figured that putting [L] would prevent the second rule being called, but it seems that it rewrites both urls in two seperate passes. I've also tried [QSE], and [S=1]
I would rather not have to move my files into different folders to get the rewriting to work, as that just seems too much like bad practise. If anyone could give me some pointers on how to differentiate between these similar urls that would be great!
Thanks
(Ref: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html)
What I would do, is make /submit-review/ post directly to itself (or a php file) then once submitted redirect from within the PHP file.
It can be hard to force htaccess to maintain post values whilst redirecting etc
My friend found a solution to this one.
RewriteRule review/submit.php - [L]
Will catch the first rewrite and then prevent the next one, worked a treat!