url rewriting and htaccess rule - .htaccess

I have rewritten a rule for URL rewriting but when i try to access the website admin panel, server shows me 404 error whereas front-end working with rewriting rule.
following rule are written
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
for admin panel
RewriteRule ^admin/page/([a-zA-Z0-9_-]+)\.html$ admin/page.php?page=$1
RewriteRule ^admin/page/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/(.*)$ admin/page.php?page=$1&$2=$3
For frontend
RewriteRule ^(.*)\.html$ $1.php [nc]
Though for front-end rule working fine and i can access the website using www.blabla.com/index.html. Written rule automatically convert the .php file into .html
Moreover these rule are working fine in my localhost and UAT server but on production server these rules do not work.
Any one can suggest what wrong i am doing or is there something with .htaccess or mod_rewrite where i can do some tweaks.
Suggest please.....

Related

htaccess URL masking subfolder to another domain instead of redirecting

I need to redirect www.domain.com/folder to subdomain.anotherdomain.com without change the first url, www.domain.com/folder. In www.domain.com I have a WordPress site and in subdomain.anotherdomain.com a Moodle site. By that, only www.domain.com/folder must have a redirect, not the www.domain.com or www.domain.com/other_folder. It can be done? It´s possible?.
In the Worpress site I´ve 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>
I try with a iframe, but a Google Chrome don´t like that :/ (cookies issues)
Many thanks in advance.
You cannot somehow keep a URL pointing to one http server visible in the browsers URL bar but make it connect to some other http server. That would be a gigantic security issue.
If you can change the setup such that both host names point to the same http server, then what you ask can be achieved by applying simple some internal rewriting rules.
For separate http servers your only option is to use the proxy module the apache http server brings, if it is installed and enabled ...
ProxyPass /folder https://subdomain.anotherdomain.com
ProxyPassReverse /folder https://subdomain.anotherdomain.com
See the documentation of the proxy module for details: http://httpd.apache.org/docs/current/mod/mod_proxy.html#proxypass
The proxy module can also be used from within the rewriting module, but this comes with an optional decrease of speed:
RewriteEngine on
RewriteRule ^/?folder/(.*)$ https://subdomain.anotherdomain.com/$1 [P,QSA,END]
Again this is documented in the rewriting module:
http://httpd.apache.org/docs/current/rewrite/flags.html#flag_p

htaccess redirect without losing url

I want my website to redirect from https://www.sitename.com/single-product/supplier-name/supplier-id/article-name/article-id to https://www.sitename.com/single-product and want the same original URL restored.
Currently, https://www.sitename.com/single-product/supplier-name/supplier-id/article-name/article-id URL gets me to 404
I know I will have to use the P flag in .htaccess file in document root to get this working. But I don't know why my solution is not working
RewriteCond %{REQUEST_URI} ^/single-product
RewriteRule ^(.*)$ /single-product/$1 [P]
I have found a solution not it is not working on my server. I guess mod_proxy is currently off on my server
RewriteEngine on
RewriteRule single-product/* https://www.sitename.com/single-product/ [P]
This is a great website to test these scripts
https://htaccess.madewithlove.be/

.htaccess Redirect from sub.domain.com.au* to www.domain.com.au/URL

I am attempting to redirect a development site that has been indexed by search engines to a specific category in the new live site.
The development site URL was:
http://staging.mydomain.com.au/
There are multiple page url's after the domain so I'd like to redirect all of these to the same new URL.
E.g.
http://staging.mydomain.com.au/essential_grid/
http://staging.mydomain.com.au/feed/
The new url is, where "/portfolio/" is where I need the old URLs redirected to:
http:www.mydomain.com.au/portfolio/
While I'm not receiving any error messages, staging.mydomain.com.au is
not redirecting to www.mydomain.com.au/portfolio/
This is a snippet of what I have in my .htaccess file at present:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} ^staging.mydomain.com.au$
RewriteRule ^(.*)$ http://www.mydomain.com.au/portfolio [R=301,L]
</IfModule>
Any help would be greatly appreciated.
UPDATE: It seems (from comments) that staging. no longer exists - that's the problem! staging. needs to exist (ie. the DNS must resolve) so that the request gets to your server in order to do the redirect. If staging. does not exist then the initial request will simply fail (the browser will fail to lookup the domain and you'll get a DNS error).
You need to recreate the staging subdomain (even with no files) and then implement the redirect below.
Your RewriteRule actually looks OK - assuming you are wanting to redirect all those URLs to the single /portfolio URL. However, the order should perhaps be changed to have the redirect before the internal rewrite. RewriteEngine only needs to be included once and you don't need RewriteBase in the code you have posted. So, this should be rewritten as:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^staging\.mydomain\.com\.au$
RewriteRule ^ http://www.mydomain.com.au/portfolio [R=301,L]
^(.*)$ is reduced to simply ^ since you don't need to capture the URL for a backreference in the substitution.
I've also removed the other RewriteRule as it doesn't appear to be doing anything?
Also note that 301 redirects are cached by the browser, so make sure that you clear your browser cache between failed attempts. It can be easier to test with 302 temporary redirects for this reason.

.htaccess URL Rewrite Failing - The requested URL was not found on this server

I have a very simple rewrite rule. My entire .htaccess file is as follows:
RewriteEngine On
ReWriteRule ^([a-zA-Z0-9\-]+)$ /index.php?page=$1 [L,NC]
This works perfectly on one of my development machines running Apache. It does not work however on my other development machine running Apache (mod_rewrite listed in PHP info under apache2handler). Nor does it work on the live server, which I think is running Windows.
I have tried adding the following:
RewriteBase /
I'm no mod_rewrite wizard and I'm sure it's a very simple solution but it is eluding me currently and keep receiving a 404 not found error.
One thing that is different on the two environments in which this doesn't work, is that the site is not in the root of the URL. So for example it is http://localhost/site and www.example.com/site
I have tried various syntax adjustments in the .htaccess file, and also adding the site to the base:
RewriteBase /site
Remove the 'RewriteBase /site' and try removing the / before index.php
The slash should't be there with use of .htaccess
RewriteEngine On
ReWriteRule ^([a-zA-Z0-9\-]+)$ index.php?page=$1 [L,NC]

htaccess rewrite all to index.html

I'm trying to write the .htaccess file so that whatever the user requests, he will have the page index.html
I've written this:
Options +FollowSymlinks
RewriteEngine on
RewriteRule .* index.html [NC]
I understand that this will cause: whatever the incoming request URL is, i.e, www.domain.com/*** (whatever comes after the slash), the result will be the page www.domain.com/index.html
However, I'm getting a Server error. What am I missing?
NOTE: I don't want it to be permanent redirect, I'm just trying to "hide" the content of my site for a couple of hours with that index.html page (which says that the site is under maintenance).
If you want to redirect everything to temporary maintenance page, you can do :
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/maintenance.html$
RewriteRule .* /maintenance.html [L,R=302]
the R=302 flag is used to generate a temporary redirect
Try to remove Options +FollowSymlinks , some servers won't allow you to overwrite a php.ini setting.

Resources