htaccess redirect to subfolder and file - .htaccess

I think there are many htaccess redirect samples but I can't find a case similar to mine.
I have many sub folder my root
/suppliers
|- /suppliers/abc
|- /suppliers/xyz
/merchants
|- /merchants/abc
|- /merchants/xzy
etc.
I want to redirect them when I type
/m/yyy -> /merchants/yyy/index.php
/m/yyy/abc -> /m/merchants/yyy/abc.php
/m/yyy/abc/ -> /m/merchants/yyy/abc/index.php
Can anyone advise me on how I can do that?

You may use these rules in site root .htaccess:
RewriteEngine On
# /m/yyy rule
RewriteRule ^m/([\w-]+)/?$ merchants/$1/index.php [L,NC]
# /m/yyy/abc rule
RewriteRule ^m/([\w-]+)/([\w-]+)$ merchants/$1/$2.php [L,NC]
# /m/yyy/abc/ rule
RewriteRule ^m/([\w-]+)/([\w-]+)/$ merchants/$1/$2/index.php [L,NC]

Related

Error when using RewriteRule on the second .htaccess file

I am developing on my localhost and i have a structure like so:
|public_html
| .htaccess
| [pagefolder]
| ......index.php
| ......[adminfolder]
| .............htaccess
| .............admin.php
As you can see, i decide it not to go crazy on .htaccess files. Just 2 is enough. One on my root and one on my protected admin folder.
when the url is localhost i just do the following on my root .htaccess to display the initial page:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^/?$ pagefolder/index.php [L]
When i want to access the admin panel (url: localhost/admin), i just do:
RewriteRule ^admin/?$ pagefolder/adminfolder/admin.php [L]
So far so good (i think). I run into problems when im using the .htaccess in the [adminfolder]
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^award/?$ admin.php?v=award [L,QSA]
When i go url: localhost/admin/award i get error:
The requested URL /admin/award was not found on this server.
However, the rule in the [adminfolder] looks ok to me. Im guessing something is missing, but not sure what?
None of your rules match /admin/award.
Your last rule redirects /pagefolder/adminfolder/award to /pagefolder/adminfolder/admin.php?v=award.
You can fix this by redirecting more admin URLs:
RewriteRule ^admin(/.*)? pagefolder/adminfolder/$0 [L]
Then change your adminfolder .htaccess:
RewriteRule ^admin/?$ admin.php [L,QSA]
RewriteRule ^admin/award/?$ admin.php?v=award [L,QSA]

How to rewrite theme path with difference in folder using htaccess

I'm having trouble with .htaccess config...
List files and folders content:
/
src/
css/
admin/styles.css
user/styles.css
js/
admin/init.js
user/init.js
I want to access the file/folder path will matching paths rewrite
http://domain/src/css/staff/styles.css => src/css/admin/styles.css
http://domain/src/css/styles.css => src/css/user/styles.css
http://domain/src/js/staff/styles.js => src/js/admin/init.js
http://domain/src/js/styles.js => src/js/user/init.js
And here is my code:
RewriteRule ^src/staff/(.*)$ src/admin/$1 [L]
RewriteRule ^src/(.*)$ src/user/$1 [L]
But apparently things not working, there was an error has occurred "500 Internal Server Error".
If you just take a look at my problem and share a bit of your science, I'd be very grateful. Thanks!
With more regex matching you can use these 2 rules:
RewriteEngine On
RewriteBase /
RewriteRule ^(src)/(js|css)/staff/([^/.])+\.(css|js)$ $1/$2/admin/$3.$4 [L,NC]
RewriteRule ^(src)/(js|css)/([^/.])+\.(css|js)$ $1/$2/user/$3.$4 [L,NC]

htaccess - dynamic Baseurl generated from subsubdomain

unfortunately I don't know how to start.
I have the following file structure:
.htaccess
|- site1
|- .htpasswd
|- index.php
|- site2
|- .htpasswd
|- index.php
So I need a dynamically baseurl depending on the sub-subdomain.
When I enter http://site1.preview.domain.com I want the baseurl pointing to site1.preview.domain.com/site1. But in browser it should still be http://site1.preview.domain.com
Furthermore I want to protect each "site"-folder. So is it possible to dynamicly point to the htpasswd?
Thanks in advance for suggestions.
In the root .htaccess you can have this rule:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(site1|site2)\.preview\. [NC]
RewriteRule !^(site1|site2)/ %1%{REQUEST_URI} [L,NC]

htaccess redirect if filename equals something

I'd like to set a redirect (preferably with RewriteCond) so that if the requested file is index.php regardless of directory, it will be redirected to another site.
So visiting /index.php or/files/index.php or /stuff/index.php (etc.) will all redirect you to another domain.
Here is a general way to do it. This rule set should be placed in the .htaccess file in the root directory:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} index\.php/? [NC]
RewriteRule .* http://otherdomain.com/ [R=301,L]
Redirects permanently any URL that holds index.php in any position, like
http://mydomain.com/index.php or
http://mydomain.com/any/folder/quantity/index.php or
http://mydomain.com/any/folder/quantity/index.php/any/folder/quantity/
To
http://otherdomain.com/
That's it. You don't explain much so nothing is passed to the other domain, just as you say in your question:
...redirected to another site.
These rules should do it (when placed inside /.htaccess file):
RewriteEngine On
RewriteRule (?:^|/)index\.php$ http://otherdomain.com/ [R=301,L]

Redirect every index.php to his root folder

I'd like to know the RewriteRule to redirect(301) every index.php to their root.
For example, this url:
http://www.example.com/folder/index.php
is redirected to:
http://www.example.com/folder/
I need a generic rule to avoid .htaccess edit for each new folder.
The .htaccess will be placed on the root of the website.
RewriteEngine On
RewriteRule ^(.*)/?index\.php(\?*.*)$ $1/$2 [L,R=301]
This should take /anyfolder/another/folder/index.php?a=1 and rewrite it to /anyfolder/another/folder/?a=1 (GET variables optional, of course.)

Resources