Apache mod_rewrite: remove only a specific folder from the url and remove the extension from all the files inside that folder - apache

I've tried some different solutions I've found on the web on my .htaccess file (placed in the root of my website), but I always end up with an "Internal Server Error"...
I need a generic rule to remove a specific folder from the URL and the extension of all the files contained in it (adding a trailing slash at the end), with a redirection to the rewrited url. So, for example:
the folder I want to work on is called "pages", so the rule should not affect any other folder, and I want that the url
http://www.example.com/subfolder/pages/
will be rewrited/redirected to
http://www.example.com/subfolder/
and
http://www.example.com/subfolder/pages/page1.php
will be rewrited/redirected to
http://www.example.com/subfolder/page1/
and
http://www.example.com/subfolder/pages/subpages/page1.php
will be rewrited/redirected to
http://www.example.com/subfolder/subpages/page1/
and so on...
How can I achieve that?

This would be more logical in 2 rules:
Remove extension: /subfolder/pages/(.*).[^.]+ -> /subfolder/pages/$1/
Remove pages from URL: /subfolder/pages/(.*) -> /subfolder/$1
Didn't test the rules but this should get you there.

Related

Redirect from a URL without a hash to a URL with a hash

On Apache, I want to create a URL redirect rule using .htaccess.
The expected incoming URL will be formed according to the following pattern:
[PROTOCOL]://[domain name]/i/[INTEGER]/[INTEGER]/[STRING]
I want the above URL to be redirected so that there is now a hash in the URL, as follows:
[PROTOCOL]://[domain name]/i/#/[INTEGER]/[INTEGER]/[STRING]
Examples
I want to go...
FROM THIS:
https://example.com/i/92/17/abcdef
TO THIS:
https://example.com/i/#/92/17/abcdef
Important: the browser must not strip out the hash in the redirected URL. I need that hash.
Why would I want to do this? Because routes for the original incoming URLs (without the hash) don't actually exist. I need javascript to examine the items that appear after the hash, and use them to make fetch requests to an API server from the browser. This is necessary because my host will only have static HTML files on it. Also I want a cosmetically-pleasing incoming URL which does not contain a hashtag (even though it will be redirected to a URL with a hashtag).
The question is, how do I achieve this with an .htaccess file that lives in the https://example.com/i/ subdirectory?
Using mod_rewrite at the top of the .htaccess file in the /i subdirectory:
RewriteEngine On
RewriteRule ^\d+/\d+/[^/]+$ /i/#/$0 [NE,R=302,L]
Since the .htaccess file is in the /i subdirectory, the RewriteRule pattern (first argument) matches against the [INTEGER]/[INTEGER]/[STRING] part of the URL-path (ie. relative to the directory that contains the .htaccess file).
The $0 backrereference contains the entire URL-path that is matched by the RewrietRule pattern.
The NE flag is required to prevent the # being URL-encoded (as %23) in the redirect response.
Reference:
https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule

.htaccess redirect to another folder

I currently am working on a project, whereby a redirect is required. However I am having issues with the redirect in the .htaccess file. I have a url which is
http://mylocalapp.local
which has a root directory of
/Users/accountname/mylocalapp
However I need to put in a redirect of /advice which will be using the root /Users/accountname/advice (example: http://mylocalapp.local/advice)
Here is the redirect in my .htaccess
RewriteRule ^advice/(.*)$ /Users/accountname/advice/$1 [L,NC,R=302]
When I put the url of http://mylocalapp.local/advice , I get a 404 error.
You need to remove that (.*/+)$ expression from the rule.
If that won't work, goto apache>config> httpd , then find Override none, change it to Override All (Thats it)

Browse zipfiles on apache webserver

I already have an awk script called viewzip.cgi which works as follows:
...viewzip.cgi/path_to_zipfile/zipfile.zip/
will show the root directory of that file,
...viewzip.cgi/path_to_zipfile/zipfile.zip/subdir/
shows a subdirectory (if present)
...viewzip.cgi/path_to_zipfile/zipfile.zip/path_to_file/file
will download one particular file.
Now what I want is omitting the "viewzip.cgi" part in the URL and an automatic redirect working as follows:
...path_to_zipfile/zipfile.zip
should download the zipfile as it would be standard behaviour, but
...path_to_zipfile/zipfile.zip/
with the trailing slash should redirect to a path like the first example, and also when trailing subdirs or files are appended.
How can I do that, if so? I have access to file system (i.e. ".htaccess") but not to apache's root configuration files. Or is there a (possibly well-known) better solution? A similar problem applies to .chm files which would be more easily browseable when unpacked on server on request. It would be nice if I don't need to repeat a redirection line for each single zipfile I have.
henni
The RedirectMatch keyword does the job.
RedirectMatch .../((?!viewzip\.cgi/).*)\.zip/(.*) http://www.../.../viewzip.cgi/$1.zip/$2

Getting Apache HTTP Server to fill in the file extension

I'd like to let people access files on my root domain directory without having to specify the file extension.
So, for example, there is currently a z9.html that a browser can access with www.mysite.com/z9.html. I would like to let people put in www.mysite.com/z9 to get the file.
The pecking order would be to look for a file of the name submitted with a .php extension, and then, if none found, look for a file of that name with a .html extension.
I don't know why my question was downvoted. It seems like a perfectly reasonable question for the Apache group. The answer is to use Apache's mod_rewrite:
To map any filename without an extension to that filename + .html place the following in .htaccess:
RewriteRule ^/([^.]+)$ /$1.html [L]

Generic .htaccess for multiple websites stored in subdirectories

My development environment is set up for using a single host (localhost). I am developing multiple websites on my machine, each stored under its own directory like this:
/var/www/site1
/var/www/site2
...
The document root is set to /var/www on my machine.
I am using URL rewriting for most of these websites and most of the .htaccess files will rewrite a sub-directory to GET parameters in different ways like this:
http://localhost/site1/home/red -> http://localhost/site1/index.php?page=home&p1=red
http://localhost/site2/index/param1/param2/param3 -> http://localhost/site2/index.php?page=index&p1=param1&p2=param2&p3=param3
I also tend to copy some of these websites under different directories and, when I do that, I have to make a lot of changes in the .htaccess files for the website that I'm copying.
I would like to know if there is a way to define a constant that contains the website's root directory (not the host's document root) and how can that be used with the rewrite rule so that I would need to change only one line of code (setting this constant to a different value) when copying a website.
Putting this in a different form, is there a way to perform rewrites that relate to a website root instead of a host / %{HTTP_HOST} (i.e. the "host" for the website being localhost/site1 instead of localhost) and how can this be done?
I have tried removing the host from each request at the beginning of the script and prepending it back at the end of the script, but this does not work with rewrite rules that use the [L] option.
Thank you!
Regards,
Lucian
You could make an htaccess file with rules like this:
RewriteEngine On
RewriteBase /site1/
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+) index.php?page=$1&p1=$2&p2=$3&p4=$4 [L,QSA]
And put this in the directory /var/www/site1, and if you want for it to apply to site2, change the RewriteBase and put the rules in /var/www/site2.