htaccess make different page acting as home page - php

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]

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

url rewrite using htaccess without redirection

I have a url like this:
http://www.localhost.com/code_category/computers/
I want to change this url to:
http://www.localhost.com/category/computers/
I don't need url redirection.
My current htaccess file looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
You only want to redirect code_category to categoryexternally and keep the path as it is internally so, try this :
RewriteCond %{THE_REQUEST} !\s/+category/ [NC]
RewriteRule ^code_category/(.*)$ category/$1 [R=302,L,NE]
RewriteRule ^category/(.*)$ code_category/$1 [L]
The above will redirect any request containscode_category/whatever to category/whatever externally and keep the internal path as it is .
If you want only request contains code_category/computers/ change it to this :
RewriteCond %{THE_REQUEST} !\s/+category/computers/ [NC]
RewriteRule ^code_category/computers/(.*)$ category/computers/$1 [R=302,L,NE]
RewriteRule ^category/computers/(.*)$ code_category/computers/$1 [L]
test it , if it is fine change 302 to 301 for permanent redirection.
Note: clear your browser cache then test it.
.htaccess file
Add this code
RewriteEngine on
RewriteCond %{HTTP_HOST} ^localhost.com [NC,OR]
# without redirect
# RewriteRule ^/code_category/computers/$ category/computers/
RewriteRule ^/category/computers/$ code_category/computers/
# redirect method
# RedirectMatch 301 ^/code_category/computers/$ category/computers/
RewriteEngine On enables mod_rewrite.
RewriteCond %{HTTP_HOST} shows which URLs we do and don't want to run through the rewrite.
In this case, we want to match example.com.
! means "not." We don't want to rewrite a URL that already includes folder1, because then it would keep getting folder1 added, and it would become an infinitely long URL.
[NC] matches both upper- and lower-case versions of the URL.
RewriteRule defines a particular rule.
The first string of characters after RewriteRule defines what the original URL looks like. There's a more detailed explanation of the special characters at the end of this article.
The second string after RewriteRule defines the new URL. This is in relation to the document root (html) directory. / means the html directory itself, and subfolders can also be specified.
For Reference click here
Hope this helps!

htaccess url rewrites and redirects not working

I have a website that was updated. I want to make the old urls work, I want to redirect some of them, so links from google search still work, and rewrite the url for other urls, using .htaccess.
For example:
one rewrite is a.com/services to a.com/consultancy
one redirect is a.com/services/a/b to a.com/consultancy
One extra comlpication: the site sends every request to /index.php because the URLs are not to physical files, but the index.php script does internal routing, using the requested path, to serve the right content.
I'm not sure if I can do a rewrite (services->consultancy) an then another rewrite (consultancy->index.php) in the same htaccess.
Trying this, I'm getting an internal server error 500 for any URL:
RewriteEngine On
#NEW
RewriteCond %{REQUEST_URI} ^services$ [NC]
RewriteRule ^services$ consultancy [L]
#LEGACY
RewriteRule (.*) ./index.php
Also tried the Redirect 301 directive but had no luck, the same error 500.
Any ideas of how to mix rewrites, redirects and the final rewrite to index.php?
Thanks!
Update
For the combination of redirect and rewrite I realized some of the rules are a little different than my original question, and those might be causing problems, here is an example:
# Redirect to show "consultancy" in the URL
RewriteRule ^services/a/b?$ consultancy [QSA,R,L,NC]
# If "consultancy" is in the URL, change it to the real path
# "consultancy" is an alias of the path that should be processed by index.php
RewriteRule ^consultancy/?$ en/consultoria [NC]
# Should get the "en/consultoria" path
RewriteRule ^ ./index.php [L]
The problem with the previous example is that I get the redirect "services/a/b" -> "consultancy" OK, but the rewrite "consultancy" -> "en/consultoria" is not done.
Any ideas?
You can use:
RewriteEngine On
#rewrite
RewriteRule ^services/?$ consultancy [L,NC]
#redirect
RewriteRule ^services/a/b/?$ consultancy [L,NC,R=302]
#LEGACY
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ 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}

.htaccess redirect based on language with wildcard

I have this on .htaccess for browser redirect based on language, to avoid another php code.
What i want is if the user enters www.example.com/ (wildcard after slash) the browser redirects to en.example.com/wildcard.
what i have so far is this for each language:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^en.example.com$ [NC]
RewriteCond %{HTTP:Accept-Language} ^pt [NC]
RewriteRule .* http://en.example.com [R,L]
this works well but if i enter www.example.com/contact it will redirect to en.example.com and not ex.example.com/contact
can you see what's missing?
You can capture .* and pass that to the rewrite URL in the RewriteRule definition as:
RewriteRule ^(.*) http://en.example.com/$1 [R,L]
Reference document: URL Rewriting Guide

Categories