.htaccess display /base1/folder/ using the content of /base2/folder.html - apache

When I open the url http://mysite.com/base1/folder/, I want it to display the contents of http://mysite.com/base2/folder.html. There is an index.html file in the folder folder.
To be clearer, the html file folder.html resides in the folder base2.
My .htaccess file does not work as expected
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/base1/([^/]+)/(.+)$ /base2/$1.html [L]
Could anyone please tell me is wrong with my .htaccess file?
Thanks!

Your regular expression looks wrong:
^/base1/([^/]+)/(.+)$
^^^ requires one or more characters
This should work (to accept anything following /folder/)
^/base1/([^/]+)/
Or this (to accept only /folder/)
^/base1/([^/]+)/$
Furthermore I would remove the two RewriteCond as your rule. (Though they shouldn't do any harm if /base1/ doesn't exist)

Related

htaccess mod_rewrite - rewrite folder and file to parameters

I have the follow folder structure for images:
http://www.localhost/memes/01/blaa.jpg
http://www.localhost/memes/02/blaa2.pg
etc.
Now I want to move the structure but the old one must still be available for PHP file.
So it should be rewritten like:
http://www.localhost/memes/01/blaa.jpg
to:
http://www.localhost/memes/?folder=01&pic=blaa.jpg
or to:
http://www.localhost/memes/?pic=blaa.jpg (ignoring the subfolder of memes)
yes it's www.localhost for what ever reason and I don't mind it so far :D
Could you please try following, written and tested with your shown samples. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^memes/([^/]*)/(.*jpg)$ memes/?folder=$1&pic=$2 [NC,L]

Site broken after modifying .htaccess file

I recently added these lines to my .htaccess file on the server:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
What I was trying to do was eliminate the need for .php after the file name. This worked wonderfully, but something else was broken.
The URL for the website was http://subdomain.something.domain/eCom/ and I had an index.php file in it, which was automatically loaded when the above link was used. But after editing the .htaccess file, the above link returns a 404 error. The page still opens correctly if I use http://subdomain.something.domain/eCom/index or http://subdomain.something.domain/eCom/index.php. I don't know what's happening. Anyway this can be resolved?
Note: I don't understand the code above about all the RewriteEngine stuff. I just copied from a website hoping for a quick fix. Any additional information about it is welcomed, if that's what's breaking my code.
Add RewriteCond %{REQUEST_FILENAME} !-d above your first RewriteCond to avoid matching directories as well.

.htaccess rewrite only files in a certain directory

I found this question which is similar to my request here, but I'm not sure how to modify it to my need
I want to ...
a) Drop the directory (edited, forgot to mention)
Note, I'm just using the directory as a container to keep this rule
separate from affecting other files. Maybe there's a better way to
work with a range of files only, such as a specific string in the
file name? Directory seemed like the simplest solution.)
b) Drop the file extension
c) Replace it with a trailing slash
... on files contained only within the /categories/ directory
So the effect is that website.com/categories/My-File-Name.php becomes website.com/My-File-Name/
This is what I've tried so far:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/categories/$
RewriteRule ^([^\.]+)/$ /categories/$1.php [NC,L]
This should work:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/categories/$1.php -f
RewriteRule ^(.+?)/?$ /categories/$1.php [L]

Forwarding all htm file extensions to a a specific file

I have been given a task to change 100's of .htm files on a site to only show the contact page. As appose to changing the files I am thinking to just use a .htaccess file and force all requests which includes a ".htm" or ".html" to the contact.htm page.
I have very limited knowledge about .ht_access files and the one I have which forces all requests through a index.php isn't working because one of the rewrite conditions is to check whether the file is real or not.
Any advice or tips will greatly be appreciated:
// My .htaccess rules at the moment:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ contact.htm [NC,L]
You can try something like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^\.]*\.htm$ contact.htm [NC,L]
This will replace everything that ends with .htm to contact.htm
EDIT: Made a mistake, checked it and fixed it
If you want to maintain your SEO and prevent link rot (which is bad), you'll want to let the client know that the old HTML file is no longer valid:
RedirectMatch 301 ^.*html?$ http://example.com/contact.html
Edit: as an interesting side note, returning a 410 might be a more appropriate response.

.htaccess Rewrite for PDFs from root to subdirectory

I'm trying to automatically redirect all .PDF and .pdf files from the website root to a subdirectory called docs.
Note that there aren't any actual PDFs on the website root, which is why I'm trying to use the !-f bit.
This is what I came up with so far, but it isn't working. I would appreciate any help.
# redirect PDF files requested on the root '/' to /docs/<filename>.(pdf|PDF)
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/(.(pdf|PDF))$ /docs/$1
Try changing your rule to:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/docs%{REQUEST_URI} -f
RewriteRule ^/?(.*\.pdf)$ /docs/$1 [L,NC]
The second condition may not be needed, it's there in case a request is made that ends with ".pdf" but the pdf isn't actually there, thus the first condition is true and there's a rewrite loop (resulting in a 500 server error).