Remove directories from URL using .htaccess - .htaccess

I've been searching around and tried many different ideas to fix the state of a URL for my client, but have had no joy.
The URL currently looks like this:
website.com/folder1/folder2/folder3/page.php
My client wants this to be showing much like the other links on the website as so:
website.com/page
I'm fine with removing the .php that's simple enough and is already written into my .htaccess file. It's just removing the 3 directories from the URL is what I cannot work out.
Before anyone asks about moving the file to a higher directory, I'm afraid this cannot be done as everything needs to be down in this 3rd directory for this page.
I believe my latest attempt is about the closest I've got to solving this, if anyone could point out my mistakes and a solution it would be much appreciated:
RewriteRule ^/folder1/folder2/folder3/(.*)$ /page$1 [L,R=301]
EDIT:
After researching more today, I think I may have confused people with this question. I think what I should have said is that I need to mask the URL, hiding the 3 directories, and showing just the domain and the page itself.
I still seem to be hitting a wall with masking too. Any advice?

After a whole weekend (and quite a bit of the week between working) I finally managed to succeed with what I wanted.
The site now displays as required:
website.com/page
Which has been redirected and masked from:
website.com/folder1/folder2/folder3/page.php
The piece of code required in the .htaccess is as follows:
RewriteRule ^page$ /folder1/folder2/folder3/page.php
Looks like I was being stupid before and had the syntax backwards, but all is well now and I'm allowed to sleep :)
Hope this helps anyone else in future with this sort of problem!

Related

.htaccess redirect to another page while retaining the complete query string

I am a little lost. I tried searching this site and the web at large, but I couldn't seem to find exactly what I need. So please forgive me if this has been answered before, I really tried to find it.
I have … inherited a .htaccess file with quite a lot of 301 redirects in the
Redirect 301 /shorthand /actual-link/actual-file.php
syntax (with a RewriteEngine On line somewhere high up in the file). I don't know exactly much about redirects, but that seems pretty straightforward to me. It just sits there and sometimes new shorthand links get added. There is some non-www to www, and http to https kind of stuff at the top, that's it.
Now the structure of the site changes, and two similar pages that process query parameters get consolidated into one. Basically there is
Old page: /path/subpath/index.php?some=query&parameter=or&other
New page: /other-path/file.php?added=parameter&some=query&parameter=or&other
I can't predict what parameters exactly will be part of the URL, I just have to take everything starting from the ? and append it to the new URL, that already has to include an added parameter, so everything after the ? follows ?added=parameter& .
I suppose that is not exactly hard, but alas, I lack the experience. And all I could find was something like "Take this specific defined query parameter you already know and set it as a path name" or vice versa, and I couldn't get that to work for my problem.
Is there a solution compatible with the syntax used elsewhere in the file? Or does that matter at all? Can I combine Redirect 301 … lines with RewriteCond … RewriteRule … commands? Does %{QUERY_STRING} help me somehow? If so, how can I figure out the correct syntax?
I would really appreciate if someone could point me in the right direction.
Many thanks in advance!

AGAIN: how to hide subdirectories in browser bar with htaccess

Sorry to bother you perhaps again, but I can't get it working after trying at least 30 answers already given on this subject!!
I use a somewhat deep directory structure and the I would like to rewrite the address browser bar of all subdirectories been replaced by one: simply (www.)example.com/subdirname. Even if I redirect from within the subdirectories to a higher level.
In other words:
So I have: http://www.example.com/subdirname ----> this what I would like to show every time. Here is also my main index.html located.
Then the structure beneath is e.g. www.example.com/subname/text/image/magazine/xxx.html
I have tried all the REWRITE CODES available (well, practically). But nothing works.
Can and will someone please give me the ultimate answer how to code this in htaccess? Please don't forget to tell me please, in which directory I should place this htaccess (allthough I tried all).
By the way, I don't care about SEO - the (sub-)pages don't have to be 'searchable'.
By the way, this is a site which I like to protect a little against theft, since it concerns my living of bookselling.
Thanks a lot beforehand!
Rokus
There is one way to do this, a frame redirect.
That'll always show the same URL in the address bar - but it's trivial to find the actual URL for anyone with the slightest bit of technical knowledge.
Users will also be unable to link to a specific page or magazine.
If you have intellectual property you want to protect, it might be worth looking into other, more suitable ways to do so.

Is it possible to redirect with .htaccess based on a date in the URL for static files?

I have a group of images that should be progressively made available on specific dates in the future until the whole set is visible. The images have the date they should be visible as the file name. Anyone with a few insights into the workings of the internet could figure out the pattern in the file names and look at 'future' images before the intended date.
I'm hoping to solve this with .htaccess to prevent spinning up a script every time an image is accessed. Do you think it's possible?
The locale of the server is the only one I'm concerned about.
Thanks :)
Edit:
As an example, at the time of posting this message I'd want this image to serve correctly:
http://domain.com/images/2012-11-20.jpg
But I'd want this one to return a 404, 401 or whatever as it's in the future:
http://domain.com/images/2012-12-06.jpg
I don't seem to fully understand your question, but this link about time-dependant rewriting might give you a little push towards the right direction.
http://www.askapache.com/htaccess/time_hour-rewritecond-time.html
(I am in no way affiliated with this website!)
Rather than having the code change the src of the image, you could dynamically rewrite a fixed image source to the correct daily location:
RewriteCond %{REQUEST_URI} /my/image.jpg
RewriteRule (.*) /images/date/%{TIME_YEAR}-%{TIME_MON}-%{TIME_DAY}.jpg [L]
It's a bit security-by-obscurity, and you lose the ability to long-term cache the image (because come the next day, you'll want the browser to load the new image from the old path), but it might fit your purpose.

Removing forward slashes after a particular subfolder section in a URL address

I have a networking site that has member data organised into subfolders. To access the data, a user would need to go to something like:-
www.domain.com/members/so/me/on/esfolder
My question is, can I use a function in the .htaccess file to allow users to visit the same page by just typing:-
www.domain.com/members/someonesfolder
I have read up on the Rewrite syntax but struggling to understand how to do this. It is simply a case of removing the forward slashes after a particular point.
Any ideas? Thanks in advance.
I've figured it out myself after a non-stop mess around lasting several hours... For those who might want to know, here is the code I used...
RewriteRule ^members/([A-Za-z0-9]{1,2})([A-Za-z0-9]{1,2})([A-Za-z0-9]{1,}) members/$1/$2/$3/ [L]
In the above, there is just three sub-folder drill downs (which is what I have decided to do rather than the four in my original question).
Hope this helps someone else!

Domain name slash user name redirect to PHP page

I would like to find a way for users to be able to type their username after my domain name to access their public profile page.
For example, if you type youtube.com/username it shows that user's channel page. The resulting page is the same as youtube.com/user/username.
So with my website, I have mydomain.example/users/profile.php?name=username
It's a bit more complicated, with PHP and variables and subdirectories and everything... I would like that same page to display when I type mydomain.example/username
I really have no idea where to start with this, but I suspect it would be something in the .htaccess file, which I do have access to.
Thanks for any help!
edit: sorry this is a few days old, I've been having some other troubles and I haven't been able to test it until now. Anyway, it isn't working...
Just to recap, I want mydomain.example/username
to filter to
mydomain.example/users/profile.php?name=username
Thanks..
edit2: I found this also on stackoverflow... link text i tried tweaking that, and what was mentioned here a few days ago... but it still won't work. Any ideas?
I also keep finding pages that recommend the strategy this guy used: link text but it looks like that would cause more problems than it's worth.
Any ideas?
RewriteEngine on
RewriteRule ^user/(.*)$ profile.php?name=$1 [NC,B,QSA]

Resources