Rewriting with .htaccess doesn't work - regex

I'm trying to make URL's look better by using a RewriteRule in .htaccess, but it doesn't work.
This is the case. I have a root folder in which there is a PHP-file called pdf.php. The script in that file sends a PDF-file to the user, which name is given as a variable in the URL. The PDF-files are in a folder called pdf, placed in the root. That folder is protected so that it's only possible to get a PDF-file by using the script. All that for security reasons.
You can download the PDF-files when you go to http://example.com/pdf.php?file=pdf/File.pdf for instance, where pdf/File.pdf is the path to the PDF-file. That's working fine. I want it to work when you simply go to http://example.com/pdf/File.pdf, so that URL has to be rewritten to the one mentioned before.
I tried to make a RewriteRule using a generator, but it doesn't work.
The following rules are in the .htaccess-file in the folder pdf. Placing it in the root didn't work either.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^pdf/([^/]*)$ /pdf.php?file=pdf/$1
The protection of the folder pdf is as follows.
ErrorDocument 403 http://example.com/
Order deny,allow
Deny from all

Try this code in root .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# to convert spaces to hyphens
RewriteRule "^(pdf)/(\S*) +(.*)$" /$1/$2-$3 [L,NE,R=302]
RewriteRule ^(pdf/.+)$ /pdf.php?file=$1 [L,QSA,B,NC]

Related

.httacces custom redirectioning

I want to redirect all invalid traffic to my index page.
Invalid traffic in this context means:
-Case 1. Nonexistent directories
-Case 2. Nonexistent files
-Case 3. Existing route but only directory: Like in example "validroute/images" or "validroute/images/"
Actually my working code is
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [R,L,QSA]
Options -Indexes
For cases 1 and 2 the conditions and the RewriteRule are redirecting correctly to my index.php
For the third case Im using the "Options -indexes" which avoids user to browse my directory and displays the forbidden error. I want to redirect this last case also to the index.php instead.
The nearest approach i got based on redirect 403 error using .htaccess was this
ErrorDocument 403 /index.php
Instead of redirecting ALL forbidden (403) when trying to display a valid "folder" (and ONLY in this case) i seek to redirect to index.
By setting the same redirects i expect the user not to be able to see my directories even if he tries to manually input different routes to see if the directory exists or not. Actually he has the hint because some are "forbidden" and others redirects to index.
This is NOT a duplicate of these because given answers didn't fit me or the issues are not what I intend:
.htaccess redirect - Options -Indexes
.htaccess option -indexes redirect?
.htaccess to redirect everything to index.php, but keep a copy of the website in a subdirectory
Maybe is not possible at all or maybe there is another command or maybe a proper regex combined to RewriteRule to fix the selection.
There is a layer above for the server at which i am not able to access or to see in any way, but as i was handling other kind of permissions (folder accesses by password) i noticed that this had its own way of working without allowing developer to modify this.
So i guess there is no way to override such from a higher lvl .htaccess without direct access to server.
I was using Plesk Obsidian.
So if you dont have full control on the server your configurations might generate some conflicts.

Apache redirect keeping URL

I'm making a JavaScript web app running on an Apache 2 server. I'm wondering if it's possible (either with mod_rewrite or some other mod) to make any path you type load the index.html from the root path, but keeping the URL?
For example: "example.com/blah/blegh" will load "example.com/index.html", but the address bar will still have "example.com/blah/blegh". Same if you tried typing "example.com/everything/is/index" would still load "example.com/index.html" and have "example.com/everything/is/index" in the address bar.
A simple answer about any mods I would need to use and which commands might be best would suffice. Though a code example would be very useful since I'm new to regex's and Apache rewriting.
Thank you for your time :)
Note: I'm doing this since I'm using History.js to parse URLs/titles into the address bar and tab titles while navigating (a one-page dynamic site). I'd like to be able to just load up the root index.html with the user's initial URL request and respond to users' actions that way much like a REST server.
Actually, you want to rewrite without redirecting. This requires enabling mod_proxy and mod_rewrite in Apache's httpd.conf.
Then, the rewrite should look like this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.html [NC,L,QSA]
Reference:
What exactly does the Multiviews options in .htaccess?
htaccess rewrite without redirect
Apache: RewriteRule Flags

.htaccess redirect root to folder

I have two independend websites on my server. Of which one website is located in a subfolder of the other. Both websites are accessible over two different domains. Following the folder structure on my server:
/www <- one.com
/index.html
/assets
...
/folder <- two.com
/.htaccess
/index.html
/assets
...
Therefore, if I want to access website two I can do so with the following URLs:
one.com/folder
two.com
My problem: Using absolute paths to include files (css/js/img) in website two, I need to adapt my root depending over which URL the user access website two. Following an example for illustration:
The html file /www/folder/index.html contains an image:
<img src="/assets/img.jpg" />
If the user access this html file via the domain one.com/folder/index.html the image source path gets translated to one.com/assets/img.jpg where it won't find any image. Correct would have been one.com/folder/assets/img.jpg (one notice the folder in the path). Obviously, if the user calls two.com/index.html the problem doesn't exist.
Therefore, I thought I would change the root (base directory) of website two via .htaccess mod rewrite depending on which URL gets called. Following the simple pseudo code I would like to implement:
if url == one.com/folder*
root = /folder/
else
root = /
And following what I got so far for my .htaccess file:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?one\.com
RewriteRule ^/(.*)$ /folder/$1 [R]
Unfortunately, it seems as if I am missing something here. So far it doesn't work. Any advice is much appreciated!
You can place this rule in /www/.htaccess folder:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(assets/.+)$ /folder/$1 [L,NC,R=301]

Need the right .htaccess configuration

Let's say I have a domain called test.com.
Now my index.html is situated in /public_html/client/frontend/questionnaire/index.html, so I can access it in my browser via http://test.com/client/frontend/questionnaire/index.html.
What I want to achieve is:
* As soon as a user enters the url http://test.com, he or she should directly access the index.html
* Restrict the user so that he can only access this index.html file and not other directories on the server.
I think it should work somehow with the correct .htaccess configuration.
Any help would be great :)
You can use this code in your DOCUMENT_ROOT/.htaccess file:
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^$ client/frontend/questionnaire/index.html [L]
Here Options -Indexes is used to disable directory listing.

Redirecting home to page in subdirectory, mask url

I'm using wordpress.
I'd like my site's domain root to display contents of another page while still showing my domain root as the url.
EXAMPLE: "http://www.mysite.com" displays contents of "http://www.mysite.com/blogs/home"
but users still see "http://www.mysite.com" in the address bar.
I've managed to get the redirect working, but it's not masking the URL. It essentially is redirecting.
Here's what I have now in my htaccess:
RewriteEngine on
RewriteRule ^\/?$ \/blogs\/home\/ [R,NC]
The R option forces an external redirect. Try just [NC] instead
what about this way.
put the index.php and .htaccess file in the root of your domain.
edit index.php to grab the "wp-blog-header.php" file from what ever location you put it. For example, if your wordpress file is located in blogs/home/ sub directory, you change the index.php file located in the root directory as require('./blogs/home/wp-blog-header.php').
forget about the redirect issue.
I think this is the easiest way. Or did I misunderstand your question?
There is a very simple solutions to this problem. In fact wordpress has an option to accomplish this task.
Go to Dashboard->Settings->Reading->Front page displays
Select a static page as front page.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^$ /blogs/home [NC,P]
PS: Make sure you have mod_proxy enabled in your Apache config.