Htaccess rewrite to file located up one level - apache

I have an htaccess in a subdomain with the following rule:
RewriteEngine On
RewriteBase /
RewriteRule ^feed([0-9]+)$ ../image.php?id=$1 [L]
So basically I want to redirect requests from feed which is located in a subdomain to image.php file which is located in the main domain (just one directory above). Using ../ doesn't seem to work like in PHP.
So how can I rewrite to file in the parent directory?
Thanks!

You can use mod_alias for this. Add the following directive to .htaccess file of subdomain:
AliasMatch "^/feed([0-9]+)$" "/absolute/path/to/image.php?id=$1"
Alternatively you can use symbolic links, but in my opinion AliasMatch directive is better.

Related

How do I host my website with the index file being in a subfolder?

I would like to host my website from /src/index.html
So if you visit my website at https://example.com, you will see the contents of /src/index.html (without adding /src/index.html to the url)
This is currently my file structure
I tried adding the following in my .htaccess file:
RewriteRule ^ src/index.html
That redirects correctly but also redirects all paths linking to any other files (like style.css) to src/index.html making them unusable.
Something like the following would work but it would change the url to example.com/src/index.html while I would like it to stay at example.com:
Redirect 301 /index.html /src/index.html
RewriteRule ^ src/index.html
This naturally rewrites everything. To rewrite requests for the root only then you need to match against ^$ instead.
For example:
RewriteRule ^$ src/index.html [L]
The easiest way is to put your project elsewhere, outside of DocumentRoot, and make a softlink (ln -s) to your src folder in DocumentRoot.
If you want your project to be the top level (as in your example, http://example.com/), then you can directly set DocumentRoot to your src folder, or replace the DocumentRoot folder with a softlink as described above.

htaccess redirect only specific page, not subpages

I have a website with following url structure:
www.example.com/products
www.example.com/products/productA
www.example.com/products/productB
I need to redirect www.example.com/products to www.example.com but www.example.com/products/productA and productB should still be available.
Does anyone have an idea?
At the top of your .htaccess file, using mod_rewrite:
RewriteRule ^products$ / [R=302,L]
I've used mod_rewrite here, as opposed to a mod_alias RedirectMatch directive since I assume you are already using mod_rewrite later in the file to rewrite your URLs. It is preferable not to mix redirects/rewrites from both modules.
Reference:
https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriterule

Redirect sub-folder to another folder

I need to redirect the content of domain.com/app1 to domain.com/apps/app1, but keep the url domain.com/app1.
How to do that?
Is it possible to do via .htaccess file, or should I modify httpd.conf file, add some VirtualHost ...?
Thanks.
You can use this rule in http or virtual host config:
RewriteEngine On
RewriteRule ^/?app1(/.*)?$ /apps%{REQUEST_URI} [L,NC]
You may use this rule in site root .htaccess as well.

How to redirect everything to a directory?

I am organizing my public_html directory. I copied all scattered files and directories from root public_html into public_html/main directory. I would like to redirect all requests into "main" directory, basically just adding "main/" to the URL. For example:
...mydomain.com goes to ...mydomain.com/main
...mydomain.com/product_page.php goes to ...mydomain.com/main/product_page.php
*notes: Just replace the ... above with "www." because I am not allowed to post links.
I tried this rule below in .htaccess but doesn't work.
RewriteEngine On
RewriteRule ^main/?$ http://www.mydomain.com/main/ [R=301,NC,L]
Is there something wrong with that?
Thanks.
hc.
RedirectMatch 301 /(.*) /main$1

How can I rewrite this URL with Apache?

I want to be able to access sitenamehere.com/folder/index?a=something by visiting sitenamehere.com/folder/something in my address bar.
How can I do this?
I've looked into mod rewrite but I don't understand it.
mod_rewrite is an Apache (web server) extension not related to PHP. You'll want to create a file called .htaccess and include the following line:
RewriteEngine On
RewriteRule ^folder/(.*) /folder/index.php?a=$1
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html
Your .htaccess or httpd.conf.
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /
# Your rule
RewriteRule ^([^/]+)/([^/]+)/?$ $1/index?a=$2 [L]
This assumes you want where folder is to be mapped over to where folder is in your example. If you want to match the literal folder, just replace the first capturing group with it (and add it to the replacement).