I am trying to redirect index.html to home.html and also remove that home.html from url via .htaccess. But it shows The page isn't redirecting properly.
Which Means i need to redirect mydomain.com/index.html to mydomain.com/home.html and I need to show mydomain.com/ only when I visit index page.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
Redirect 301 "/index.html" "/home.html"
RewriteCond %{THE_REQUEST} ^GET.*home\.html [NC]
RewriteRule (.*?)home\.html/*(.*) /$1$2 [R=301,NE,L]
</IfModule>
What you could try instead of utilizing mod_rewrite is setting DirectoryIndex to the desired page (in your case home.html):
DirectoryIndex home.html
So, if you're calling domain.com/ it should give you the contents of domain.com/home.html
For reference, the Apache configuration manual:
https://httpd.apache.org/docs/2.4/mod/mod_dir.html
"DirectoryIndex Directive"
Referencing the conversation right below:
Maybe this could work (Sorry, I cant test it right now)
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} =/index.html
RewriteRule (.*?)home\.html/*(.*) /$1$2 [R=301,NE,L]
A bit of explanation: If the requested host matches example.com and the called URI is in fact not a real file or folder plus the URI equals to
"index.html" it should redirect to home.html.
EDIT: As you dont want to show the home.html we can maybe get away by "proxying" the request internally instead of forcing an external redirect. Try to replace "R=301" in the last line (the RewriteRule) with "P" so it says RewriteRule (.*?)home\.html/*(.*) /$1$2 [P,NE,L]
// It's working code i hope it's useful ..
<IfModule mod_rewrite.c>
RewriteEngine On
Redirect 301 / http://newsite.com/
</IfModule>
or
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?http://dbdenterprise\.in$
RewriteRule ^(.*)$ http://dbdenterprise.com/$1 [R=301,QSA,L]
Related
I need a help on a site redirection, i have 2 domains : mywebsite.com and mywebsite.org, they both point to the same site (wordpress), and the .com is already indexed by google, i want to redirect all *.com links to www.mysite.org links also all non www of .org to www.mywebsite.org
I have done like this but nothing works :
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS_HOST} ^(www\.)?mywebsite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mywebsite.org/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
What i have done wrong here?
thank you
RewriteCond %{HTTPS_HOST} ^(www\.)?mywebsite\.com$ [NC]
RewriteRule ^(.*)$ http://www.mywebsite.org/$1 [R=301,L]
As noted in comments, HTTPS_HOST does not exist, it should be HTTP_HOST. So, the above condition will simply fail to match and no redirect occurs.
So, if you correct the above variable then this should work to redirect all traffic from mywebsite.com to www.mywebsite.org, maintaining the URL-path.
However, since this is WordPress, you should avoid manually editing the directives that are part of the front-controller, ie. the code between the # BEGIN WordPress and # END WordPress comment markers. Any redirect that you add should go before the # BEGIN WordPress block at the top of the `.htaccess file.
This also doesn't redirect the non-www version of the .org domain.
If you only have these 2 domains then you can simplify the redirect by checking that the requested hostname is not the canonical hostname (ie. is not www.mywebsite.org) and redirecting accordingly.
Try the following instead:
Options +FollowSymlinks
# Canonical redirect
RewriteCond %{HTTP_HOST} !^www\.mywebsite\.org$
RewriteRule (.*) http://www.mywebsite.org/$1 [R=302,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
<IfModule>
# END WordPress
The ! prefix on the CondPattern negates the regex, so the condition is only successful when it does not match.
Test with 302 (temporary) redirects and only change to 301 (permanent) when you are sure this is working OK - to avoid potential caching issues.
Make sure you have cleared your browser cache before testing.
Note that you are redirecting to HTTP (not HTTPS) - is that intentional?
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!
I'm looking for a way to disable access to file, if not accessed through slug. for example, I have /members.php?page=login. I want to change it, by forcing the user access /login (redirect the long url into a short slug) and prevent direct access from the long url.
When I try to do something like:
RewriteRule ^members.php?page=login login [L,R=301]
RewriteRule ^login members.php?page=login [L]
It turns into an infinite loop (as firefox says: "Firefox has detected that the server is redirecting the request for this address in a way that will never complete.").
I've enabled the "options" at the beginning of file (Options +FollowSymLinks -MultiViews)
How can I make it right?
Thanks.
You can use the following :
RewriteEngine on
# externally redirect "/members.php?page=login" to "/login"
RewriteCond %{THE_REQUEST} /members\.php\?page=login [NC]
RewriteRule ^ /login [L,R]
#rewrite "/login" back to "/members.php?page=login"
RewriteRule ^login members.php?page=login [L]
try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^login$ index.php?page=login [L]
as i want to redirect all dynamic urls like http://www.mysite.com/play.php?id=4820 to http://www.mysite.com/
i have so many dynamic urls i dont want my user to see the page like page not found etc. so whenever a user try to access the dynamic url like the above he should be redirected to home page. please can anyone tell me how to achieve this using .htaccess
Regards,
phphuger.
Enable mod_rewrite and put this code in your .htaccess under DOCUMENT_ROOT:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+play\.php[\s\?] [NC]
RewriteRule ^ / [L,R]
I think this should do.
RewriteRule ^\?.*$ http://www.mysite.com
EDIT2
Ok, now I think understand your question.
You have url at play.php that used to handle all your urls and you want them to be redirected to the homepage.
Here are two possible solutions. This is the first:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/play.php* [NC]
RewriteRule . /? [L,R=301]
</IfModule>
The above will redirect any requests to play.php to your home page using a 301 redirect, which means the redirect is permanent. This is different than a 302 redirect which is temporary. So it is important to redirect using 301.
Also in this rewrite rule, we don't care if the url exists or not. So you don't need to have the actual play.php file or anything. It just matches based on the url.
On the line for RewriteRule, there is a question mark at the end, this is to erase the query string from the url on redirect. I'm assuming you don't want the query string to carry over.
The [NC] is for case insensitive matching, so /PlAy.php would be redirected also
An alternative is use the Redirect directive:
Redirect 301 /play.php http://www.mysite.com/
This will do a 301 permanent redirect to your homepage if the user tries go to play.php. The only downside of this is that the query string shows up. You can add a question mark at the end and it will erase the query string. Unfortunately the question mark stays.
If you happen to have multiple endpoints and not just play.php, you can do the following:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/play\.php* [NC, OR]
RewriteCond %{REQUEST_URI} ^/another_url\.php* [NC]
RewriteRule . /? [L,R=301]
</IfModule>
This RewriteRule will match play.php or another_url.php and do 301 redirect to your home page.
Alternatively:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(play|another_url)\.php* [NC]
RewriteRule . /? [L,R=301]
</IfModule>
Here is the alternative using the Redirect directive
Redirect 301 /play.php http://www.mysite.com/
Redirect 301 /another_url.php http://www.mysite.com/
Alternative you could also use the RedirectMatch directive to use regex expressions (Note I haven't actually tested this one)
RedirectMatch 301 /(play|another_url)\.php http://www.mysite.com/
EDIT
Ignore this answer. I misread your question. The following redirects all requests to index.php unless the file or directory exists, which is not what you want now that I read your question.
Here is how Wordpres does it. I believe Zend and Joomla also have variations of this.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
i need to hide the extensions of my webpage and also want to let the user put the links in both (lower and upper) case:
Example:
the file name is demo.php
www.example.com/demo
www.example.com/DEMO
www.example.com/Demo
Running PHP in a LAMP server, no access to php.ini, just .htaccess
Actualy im using a file like this:
RewriteEngine On
RewriteBase /
RewriteRule ^/(OUTSOURCING|outsourcing|Outsourcing)$ outsourcing.php [NC,L]
And i m reciving this error:
Not Found
The requested URL /outsourcing was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
RewriteEngine On
RewriteBase /
www.example.com/DEMO www.example.com/Demo or doing www.example.com/DEMO/page2
RewriteRule ^/(DEMO|demo|Demo)/(.*)$ demo.php?=$1 [NC,L]
RewriteRule ^/(DEMO|demo|Demo)$ demo.php [NC,L]
RewriteRule ^/(D|d)emo$ demo.php [NC,L]
or pass anything www.example.com/DeMo www.example.com/bob to demo.php
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^\.]+)$ demo.php [NC,L]
you may want to test if your allowed .htaccess RewriteRule /*$ http://google.com [R][L]
here is a good way to do it with case insensitive method
EDIT:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ${lc:$1}.php [NC]
this way anything entered will be redirected to a php file.
edit : this way your js and css file can still run
RewriteRule ^/[Dd][Ee][Mm][Oo]/?(.*)$ demo.php [NC,L]
will redirect any capitalization of "Demo" to demo.php
First add this line in the <VirtualHost> section OR at the end of your httpd.conf file (to enable lc function in .htaccess for later use):
RewriteMap lc int:tolower
Then have these rules in .htaccess file:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET\s.+\.php [NC]
RewriteRule ^(.+)\.php$ /$1 [NE,R=301,L,NC]
RewriteCond %{REQUEST_URI} !\.php$ [NC]
RewriteCond ${lc:%{REQUEST_FILENAME}}.php -f
RewriteRule . ${lc:%{REQUEST_URI}}.php [L]
First rule in .htaccess is doing external redirect by making a URI of /index.php to /index
Second rule in .htaccess is doing internal redirect by makign a URI of /INDEX to /index.php by lowercasing the URI. Assuming you have filename index.php physically present.
That way you can always write URLs without .php extension.