how to rewrite url index.html to /home in htaccess - .htaccess

i want to rewrite url with index.html to /home, right now my url is like this https://xxxx.com/index.html, i want to convert it as https://xxxx.com/home , can anyone please help me how to do this in htaccess ? here i did some work for it but it is not working can anyone please help me how to do it ? any help will be really appreciated, this is what i tried with the code
CheckSpelling Off
AddType x-httpd-php56 .php
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^index\.html$ /home [NC,R,L]

Nearly perfect, but you need to add the internal rewriting the other way 'round... The incoming request is what you want to rewrite, so a request to /home...:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,END]
RewriteRule ^/?index\.html$ /home [R=301,END]
RewriteRule ^/?home/?$ /index.html [END]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a distributed configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a distributed configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using distributed configuration files (".htaccess"). Those distributed configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Related

Mod-rewrite rule which just gets the part of the URL after the =

I'm currently trying to redirect this URL
http://dev.example.org/active/researchers/contact.php?IDENT=12345
to
http://portaldev.example.org/users/ident/12345
in htaccess.
However, I can only get a redirect to
http://portaldev.example.org/users/ident/IDENT=12345
because I can't find a way to get rid of the IDENT=. How can I do that?
The rewrite conditions in my htaccess are:
RewriteCond %{REQUEST_URI} ^/active/researchers/contact\.php$
RewriteCond %{QUERY_STRING} ^IDENT=([0-9]*)$
RewriteRule ^(.*)$ http://portaldev.example.org/users/ident/$2 [R=302,NC,L]
This probably is what you are looking for:
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)IDENT=(\d+)(?:&|$)
RewriteRule ^/?active/researchers/contact\.php$ http://portaldev.example.org/users/ident/%1 [R=302,QSD,NC]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
This implementation will work likewise in the http servers host configuration or inside a distributed configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a distributed configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using distributed configuration files (".htaccess"). Those distributed configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
Got the answer to my question for anyone else who needs help:
RewriteCond %{REQUEST_URI} ^/active/researchers/contact\.php$
RewriteCond %{QUERY_STRING} ^IDENT=(.*)
RewriteRule (.*) http://portaldev.cepr.org/users/ident/%1? [R=301,L]
I think the main thing was the ? at the end of the rewrite rule but it also wouldn't work unless I put in the first RewriteCond to request uri and used (.*) in the rewrite rule

htaccess rewrite rule with condition and rewrite

I have a site that currently redirects to the public folder from root /
like so...
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
RewriteRule ^(.*).php$ /$1 [R=301,L]
I would like to add an exception for a system folder that needs to redirect to a different folder named 000999
RewriteCond %{REQUEST_URI} !/system [NC]
RewriteRule ^system/(.*)$ /000999 [L,NC]
However the exception whether placed after "Rewrite Base" or anywhere else fails to redirect
From your description and the example I understand that if the "folder" system is requested, regardless of the path below, there should be an internal rewrite to the folder /000999? Not a redirection? And the path should be ignored?
This should do, note that I also fixed a couple of other issues with your current setup:
RewriteEngine On
RewriteBase /
RewriteRule ^/?(.*)\.php$ /$1 [R=301]
RewriteRule ^/?system/(.*)$ /000999 [END]
RewriteRule ^/?(.*)$ /public/$1 [END]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This implementation will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Rewriting .htaccess to remove sub-directory from URL

I want to mask a folder in URL, so instead of www.mysite.com /employers/university-of-worcester/profile.html there would be www.mysite.com /university-of-worcester/profile.html , meaning that sub-directory "employers" is hidden.
There will be many folders created inside "employers" folder.
This is a matter of modifying .htaccess, I have tried a lot of solutions that I have found on stack.
The latest line of code I tried to add is:
RewriteRule ^employers/(.*)$ /$1 [L]
My .htaccess looks like this now:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteRule ^employers/(.*)$ /$1 [L]
</IfModule>
If I go to URL www.mysite.com/employers/ this will redirect to www.mysite.com/ (the homepage), which I am happy about, but if I try www.mysite.com/university-of-worcester/profile.html I get "The page can’t be found.", but the home profile.html is definitly inside "university-of-worcester" folder.
I am using wordpress.
This probably is what you are looking for:
RewriteEngine on
RewriteRule ^/employers/?(.*)$ /$1 [R=301,QSA]
RewriteCond %{REQUEST_URI} !^/employers/
RewriteRule ^/?(.*)$ /employers/$1 [END,QSA]
This will redirect direct requests to that folder and internally rewrite requests to that folder.
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

How to redirect if URL contains certain word?

Project has folder API where two files named index.php and mobile.php.
index.php is contains APIs for web platform and mobile.php for mobile devices.
I have this problem. If code will do response to /api/get-courses, htaccess should redirect to index.php file. If response url has /api/mobile.php/get-courses mobile.php word, it shoul be redirected to mobile.php
RewriteEngine On
RewriteCond %{REQUEST_URI} mobile\.php
RewriteRule ^ mobile.php [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
There i have tried to do implementation.
It is a bit unclear what you actually mean by "If code will do response to ..." ... If that refers to a request to that URL which your want to internally rewrite, then this should do what you ask:
RewriteEngine on
RewriteRule ^/?api/get-courses$ /index.php [END,QSA]
RewriteRule ^/?api/mobile\.php/get-courses$ /mobile.php [END,QSA]
This implements an internal rewrite, not a redirection. I assume this is what you actually want, according to the wording in your question. If I miss understood what you actually ask then you need to take the time to revise your question to be more precise...
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

using htaccess rewrite dynamic urls & category url

I want to know how to match a dynamic URL to a static URL using htaccess. but its's work from one RewriteRule but it doesn't work with second RewriteRule. How can i fix this.
I want it like this
contact.php to example.com/contact
category.php?url=some-url to example.com/some-url
My htaccess file
<Files .htaccess>
order allow,deny
</Files>
Options -Indexes
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteRule ^([^/]*)$ category.php?url=$1 [NC,L]
RewriteRule ^([^\.]+)$ $1.php [NC,L]
There are a number of issues with your approach. Most important: you appear to have understood the logic of rewriting rules the wrong way round...
Here is an example to get you started, you probably with have to adapt it to your specific setup:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^/?(.+)$ $1.php [END]
RewriteCond %{QUERY_STRING} (?:^|&)url=(.+)(?:&|$)
RewriteRule ^/?category\.php$ /%1 [END]
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup.
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).

Resources