.htaccess URL rewrite rules folder - php

I have this in my html folder
--html
--.htaccess
--app
--application_name
--public
--index.php
--landing_page
--index.php
As of now, everytime the website url is accessed (www.website.com), the browser opens the landing_page/index.php.
How can I make it that when I access www.website.com/beta, I will be doing a request to html/app/application_name/public/index.php
I have tried adding this to the .htaccess
RewriteRule ^beta/?$ app/application_name/public/index.php [L,NC]
This is the whole .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^beta/?$ app/application_name/public/index.php [L,NC]
But it does not work. Not sure what term to use to search for this kind of problem.

Your root is currently set at /html, and therefore the RewriteBase / is at /, the /beta/ simply does not exist. You could change the RewriteBase /app/application_home/public/ and set the RewriteRule ^beta/?$ index.php [L,NC]. This however no longer give you the access to the landing page, not sure if this is what you want?

Related

Redirect always to index.php

I'm trying to relocate a site into a temporary folder in order to install a CMS in the root. The redirect works but all the internal links in the site now take me back to the index page in the temporary folder. The .htaccess in the root is as follows:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/old/ [R=301,NC]
it's because you're redirecting everything to the new url. You should put something like this:
RewriteRule ^$ http://www.example.com/old [R=301,NC]
RewriteRule ^(.*)$ http://www.example.com/old/$1 [R=301,NC]
PS: The $1 take the arguments in expression
PS2: You should not use code 301 for a temporary redirect, the 302 is more appropriate https://en.wikipedia.org/wiki/HTTP_302

How to redirect homepage to subfolder without changing URL (still keep www.example.com)

My current .htaccess is in httpdocs.
How can I configure it to help domain point to subfolder is /vn/
and still keep: www.example.com instead of www.example.com/vn/
Try the following near the top of your .htaccess file in the document root:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.*) /vn/$1 [L]
This internally rewrites all requests that hit the document root to the /vn/ subdirectory. The RewriteCond directive that checks against the REDIRECT_STATUS environment variable ensures the request is only rewritten once - thus avoiding a rewrite loop.
In .htaccess add these lines :
RewriteEngine On
RewriteRule !^blog/ /blog%{REQUEST_URI} [L,R=301]
Or in some hosts :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www.)?YourDomainName.com$
RewriteRule ^(/)?$ blog [L]

Redirect all folders to subdomains htaccess

I am trying to redirect every folder from domain.com to it's subdomain.
Examples:
domain.com/a -> a.domain.com
domain.com/b -> b.domain.com
domain.com/about -> about.domain.com
So basically, every folder redirected to it's subdomain. For the first example, folder a contains index.html. When I browser to domain.com/a it should redirect the url to a.domain.com but use the index.html file from the a folder.
My current htaccess looks like this:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$
RewriteCond %{REQUEST_URI} !^/%1/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /%1/$1
RewriteRule ^(/)?$ %1/index.html [L]
What works
For now, if I navigate to test.domain.com, it's working, it's taking the index.html file from the test folder. If I go to domain.com/test it's still working, but I want to disable this. I want to redirect domain.com/test to test.domain.com as well, but still use the index.html files from the test folder.
Another thing that is in the htacces file is to redirect www to non-www.
Any suggestions would be appreciated.
EDIT:
Forgot to mention that I've tried to add this line:
RedirectMatch 301 ^/test/(.*)$ http://test.domain.com/$1
It's doing the redirection from domain.com/test to test.domain.com/index.html, but it's not loading the page right, I get an error (Page isn't redirecting properly).
keep like
RedirectMatch 301 ^{RELATIVE PATH}$ {ABSOLUTE_PATH}

subdomain gives access to folder

I have created one sub-domain. It is also accessed via folder.
For example,
subdomain.website.com
which is also accessed in the following url
website.com/subdomain
How do i stop this? I think it can be done with htaccess. I am not sure.
In the htaccess file in website.com's document root, add these rules:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^subdomain.website.com$ [NC]
RewriteRule ^subdomain/?(.*)$ http://subdomain.website.com/$1 [L,R=301]
add following line in the .htaccess in your root directory:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^subdomain\.domain\.com$ [NC]
RewriteRule ^/(.*)$ http://subdomain.domain.com/$1 [L,R=301]

.htaccess Pretty URL rewrite for PHP pages in a subfolder?

I've got the following directory setup:
http://www.mysite.com/public/
I'm using the following rewrite to remove the 'public' folder from visible URLs:
#rewrite the URL to display the subdirectory as the main directory for all links
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com
RewriteCond %{REQUEST_URI} !^/public
Rewriterule ^(.*)$ /public/$1 [L]
This works great, and redirects everything correctly. However, I also want to rewrite some of the dynamic pages that live in the 'public' subfolder as well, but am having trouble getting any of the rewrites I've found to work in conjunction with the above rule.
For example, with the above subdirectory rewrite rule in place, going to a URL like:
http://www.mysite.com/item.php?id=1&name=item_name
...should be rewritten to something like:
http://www.mysite.com/items/item_name
Thoughts?
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
#rewrite the URL to display the subdirectory as the main directory for all links
RewriteCond %{HTTP_HOST} ^(www\.)?mysite\.com$ [NC]
Rewriterule ^(?!public/|prints/)(.*)$ /public/$1 [L,NC]
RewriteCond %{THE_REQUEST} /*item\.php\?id=([^&]*)&name=([^&]*)\s [NC]
Rewriterule ^ /prints/%1/%2? [R,L,NC]
Rewriterule ^prints/([^/]*)/([^/]*)/?$ public/item.php?id=$1&name=$2 [L,NC,QSA]
PS: Make sure your static includes like css, js, images etc have absolute path rather than a relative one.

Categories