.htaccess to rewrite to sub directory - preserving page - regex

My directory setup is as follows:
Root directory at what.ever.ip (/var/www/)
User's sites are what.ever.ip/username
Users root ftp are /var/www/username/upload_files_here,
which translates to what.ever.ip/username/upload_files_here
So if user Foo uploads the file index.html he'll have to visit what.ever.ip/Foo/upload_files_here/index.html
I want to use apache .htaccess to rewrite this (without redirecting aka showing it) to display as what.ever.ip/Foo/index.html
I have tried multiple solutions without finding one that solve my problem.
So my question is how do I rewrite "what.ever.ip/username/upload_files_here/index.html" to "what.ever.ip/username/index.html" without showing it?
PS. The what.ever.ip is not known, could be an local address, localhost or an remote address. So I would prefer a possible wildcard instead of hard coding in "what.ever.ip"
Thanks, Alex.

Place this rule in /var/www/.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/((?!upload_files_here/).*)$ /$1/upload_files_here/$2 [L,NC,QSA]

The following:
RewriteEngine On
RewriteRule ^(.*)/upload_files_here/(.*)$ /$1/$2 [L]
should change:
what.ever.ip/username/upload_files_here/index.html to what.ever.ip/username/index.html while showing still what.ever.ip/username/upload_files_here/index.html in the URL bar.

Related

.htaccess URL redirect old link format to new

Recently I've changed the link format to articles on my self-hosted wordpress blog and I want old bookmarks to be able to redirect to the new links seamlessly, so that readers don't get page not found.
The old format was:https://domain.tld/index.php/yyyy/mm/dd/title-of-post
The new format is: https://domain.tld/title-of-post/
I made this change of course because it's a lot nicer and time isn't really a factor for my blog. I'm using Apache's RewriteRule directive in the .htaccess file but I'm having trouble getting the pattern to match. This is the current state of the .htaccess file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php/[0-9]+/[0-9]+/[0-9]/.*?$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
But previously I've tried such patterns as:
^index\.php/?([-a-zA-Z0-9_+]+)/?([-a-zA-Z0-9_+]+)/?([-a-zA-Z0-9_+]+)/.*?$
and
^index\.php/2018/03/30/.*?$
and even
(index)(\\.)(php)(\\/)(\\d)(\\d)(\\d)(\\d)(\\/)(\\d)(\\d)(\\/)(\\d)(\\d)(\\/)((?:[a-z][a-z]+))
Of which the last I tried to use txt2re
None of this gave me any luck in matching the URL I'm giving it. I want old links to redirect to the new corresponding link.
Instead of modifying your .htaccess its a better idea to use Redirection plugin such as the following,
https://wordpress.org/plugins/redirection/
it will take care of all the redirection for you
This worked for me and many others. Please let me know if this works for you.
Edit:
For your specific case, following rule will work
RedirectMatch 301 ^/index\.php/([0-9]+)/([0-9]+)/([0-9]+)/(.*)$ http://yourdomain.com/$4
You can read more on this rule here,
https://perishablepress.com/redirect-wordpress-date-archives-htaccess/

RegEx for just displaying items in private folder

I want to have an regular expression witch just allows to access files in a private folder. This is my code so far:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !(.*)(/public/)(.*)
#RewriteCond %{REQUEST_FILENAME} !-f #not active in this example
RewriteRule . index.php [L]
It works fine as far as I can see it's working fine but there is one thing I'm wondering about:
if there is an directory structure like /localhost/test/public and I'm calling the same url I can see the folder content, but if there isn't a public folder localhost/test/ and I'm calling /localhost/test/public it will lead to the index.php
Why isn't it displaying a 404 page?
I think UnskilledFreak is right:
If you don't put a slash (/) at the end of an url, the server "thinks" you want a file and checks if it exists. If its a directory, it'll automatic use that; if not, your rewrite of filename will match and redirect to index.php

Rewrite URL using .htaccess call without directory name

I really can't understand how to write htaccess lines. I was trying to Google for few hours and couldn't find anything relevant. Can anybody suggest me a simple .htaccess line that can let me navigate to http://www.mydomain.com/mydirectory/index5.html
by calling it as http://www.mydomain.com/required
I tried in the below manner but I didn't work for me.
RewriteEngine On
RewriteRule ^required mydirectory/index5.html [NC,L]
And, an other question if I place this .htaccess file in the mydirectory folder will that work or am I supposed to place this file only in the root folder?
Thanks.
Put this code in .htaccess in DOCUMENT_ROOT:
RewriteEngine On
RewriteBase /
RewriteRule ^required/?$ /mydirectory/index5.html [NC,L]
PS: You cannot keep this code in /mydirectory since your original URI doesn't contain /mydirectory
Reference: Apache mod_rewrite Introduction

How to show always the same url?

I am using Apache.
I have www.example.com/?p_action=user_profile&post_author=34 and I want to use www.example.com/niceurl/
I want that if user enters any of the urls, the www.example.com/niceurl/ is shown in his browser (of course, www.example.com/?p_action=user_profile&post_author=34 is actually retrieved from the server).
RewriteRule does not seem to be the solution for this, does it?
Thanks
Well you can try putting,
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /?p_action=$1&post_author=$2 [L]
In your .htaccess file.
IMO RewriteRule should solve your problem.

.htaccess with 2 parameters messing up css, js, images paths

I was working with URLs on my webpage but I can't solve issue for URLs with 2 parameters.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-z\-]+)/?$ index.php?strona=$1 [L]
RewriteRule ^([a-zA-Z0-9-z\-]+)/([a-zA-Z0-9-z\-]+)/?$ index.php?strona=$1&id=$2 [L]
URLs seem fine except that when my current URL has 2 parameters (for example I'm on http://example.com/subpage/5 whole webpage is broken (stylesheets, navigation etc) because .htaccess changed all links to:
(for example navigation):
http://example.com/subpage_with_2_parameters/home
instead of
http://example.com/home
Pages with one parameter (example: http://example.com/contact) work fine.
Only solution (which is horrible) I have on mind are absolute links.
You're not the only one dealing with this problem of css, js, images paths getting messed up after implementing so-called pretty URLs. I am seeing these problems being reported on SO almost every day.
You can solve this problem in 3 ways:
Best solution is to use absolute paths for images, css and js files i.e. start your path with / or http://
Another option is to use base href tag in HTML head section like this:
<base href="http://www.example.com/">
3rd option is via mod_rewrite
Put these lines above your other RewriteLine in your .htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{DOCUMENT_ROOT}/$1 -f
RewriteRule ^[^/]+/([^.]+\.(?:js|css|jpe?g|png|gif))$ /$1 [L,R=301,NC]
It's not your rewrite rules which are breaking your stylesheets, but your actual HTML. The same thing would happen if you had an actual directory called foo and placed index.php in there.
If you write Home, that link is relative to the current directory (in URL-space) so on a page with a URL like http://example.com/foo/bar/baz it links to http://example.com/foo/bar/home.
What you want instead is for the link to be relative to the root of your domain; for that, you need a leading slash: Home
The only reason this seemed to work before is that all your URLs were in the root directory, so "current directory" and "root of domain" were the same thing.