Redirect always to index.php - 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

Related

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]

.htaccess URL rewrite rules folder

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?

htaccess make different page acting as home page

If user goes to URL
http://example.com
The server does a 302 Moved and goes to http://example.com/en/home/my-page.html
What I want is if URL is
http://example.com/en/home/my-page.html
The browser should just show http://example.com/en/
I've tried with .htaccess like so:
RewriteEngine on
RewriteCond %{THE_REQUEST} ^en/home/my-page\.html
RewriteRule ^en/ /en/home/my-page\.html [R=301,L]
RewriteRule ^index.php$ en/home/my-page\.html [L,R=301]
but it does nothing. What am I doing wrong?
Your regex patterns are wrong.
You can use these rule in site root .htaccess:
RewriteEngine on
# externally redirect /en/home/my-page.html to /en/
RewriteCond %{THE_REQUEST} \s/+en/home/my-page\.html[?\s/]
RewriteRule ^ /en/ [R=301,L]
# internally rewrite /en/ to /en/home/my-page.html
RewriteRule ^en/?$ en/home/my-page\.html [L,NC]
Don't forget to clear your browser cache before testing this change.
This code will redirect user hitting
http://example.com/en/my-page.html
to
http://example.com/en/
and than will internally redirect thr request to index.php (you can substitute parameter with the directory where index.php is located or just remove it if it is in the main dir)
RewriteRule ^en/my-page\.html$ /en/ [L,R=301]
RewriteRule ^en/ /<dir>/index.php [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}

How to use htaccess?

<IfModule mod_rewrite.c>
RewriteEngine On
</IfModule>
this is on my server (mypage.com) in .htaccess file.
How can i make:
If i open here.mypage.com then this redirect me to mypage.com/here.html
If i open mypage.com/now then this redirect me to mypage.com/now/test.html
My page is in PHP.
1.If i open here.mypage.com then this redirect me to mypage.com/here.html
As long as here.mypage.com is the same document root the htaccess file is in, add:
RewriteCond %{HTTP_HOST} ^here\.mypage\.com$ [NC]
RewriteRule ^$ http://mypage.com/here.html [R=301,L]
2.If i open mypage.com/now then this redirect me to mypage.com/now/test.html
Add:
RewriteCond %{HTTP_HOST} ^mypage\.com$ [NC]
RewriteRule ^now$ /now/test.html [L]
RewriteEngine on
RewriteRule ^now$ now/test.html
That's for the second one. The first one, you'll have to use your website cPanel (or similar) make a new subdomain, and point the subdomain to the main public_html folder, but we'll likely need more information about your server to give exact help.
EDIT:
RewriteCond %{HTTP_HOST} ^here.mypage.com [NC]
RewriteRule ^/here.html [L]
That should do the subdomain.
to redirect here.mypage.com to mypage.com/here.html, this will require more than to add a rule to the .htaccess (see => Setting a subdomain).
For the other one you can add :
RewriteRule ^now$ now/test.html
Give this a try:
Redirect 301 here.mypage.com mypage.com/here.html
Redirect 301 mypage.com/now mypage.com/now/test.html

Categories