How to write a 301 redirect from an asp page to a php page in a htaccses file - .htaccess

This code is not responding:
RewriteEngine On
RewriteRule ^/index.asp?lang=he&cid=270$ /?aid=2260 [R=301,L]

The query string ("http get arguments") is not part of the URL when the rule pattern is compared against it. You need to use a separate condition to match it:
RewriteEngine On
RewriteCond %{QueryString} ^lang=he&cid=270$
RewriteRule ^/?index\.asp$ /?aid=2260 [R=301,L]
Also note the additional ? near the start of the rules matching pattern. The URL is compared as relative (so without leading slash) in dynamic configuration files. The additional ? makes that leading slash optional, so the rule now will work in dynamic configuration files (".htaccess" style files) and likewise in the real http servers host configuration.
And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files (.htaccess style files). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

this code made my redirect:
RewriteCond %{QUERY_STRING} ^lang=he&cid=270($|&)
RewriteRule ^/?index.asp$ /?aid=2260 [R=301,L]

Related

Change URL ending with Query String

I've been using a set of redirect rules for a while that have been working perfectly.
I've recently expanded a part of my website and need to change the ending of a certain URL.
Old URL: /clan/{query-string}/tracking/war
New URL: /clan/{query-string}/tracking/warlog
I've changed my .htaccess file so the new URL works, but I need the old URL to redirect to the new one.
Currently, this is how I'm redirecting in .htaccess:
# Rewrite Clan Tracking-Warlog URL
RewriteEngine On
RewriteCond %{THE_REQUEST} /clanTracking_main.php\?name=([^\s]+) [NC]
RewriteRule ^.+$ /clan/%1/tracking/warlog [L,R]
RewriteRule ^clan/([^/]+)/tracking/warlog clanTracking_main.php?name=$1 [L]
It works perfectly but I just need help with the redirection.
Thanks for your help in advance!
I'd say the first rule below is what you ask...
I also made some other modifications which appeared to make sense to me...
# Rewrite Clan Tracking-Warlog URL
RewriteEngine On
# redirect old to new
RewriteRule ^/?clan/([^/]+)/tracking/war$ /clan/$1/tracking/warlog [R=301]
# pick name from get argument and redirect
RewriteCond %{QUERY_STRING} (?:^|&)name=([^\s]+)(?:&) [NC]
RewriteRule ^/?clanTracking_main\.php$ /clan/%1/tracking/warlog [R=301]
# rewrite to php
RewriteRule ^/?clan/([^/]+)/tracking/warlog$ clanTracking_main.php?name=$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 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).

How can I apply manual rules in htaccess with one global rule?

I am using OpenCart for my e-commerce software and I have the following requirement I want to have for my store. Certain URLs I wish to apply my own rules before the normal OpenCart rules.
I tried a few tutorials online and the best I have come without causing a 500 is the following - I am placing the URL rules before the Opencart generic one. But it returns 404 in the browser - at least it is not a 500.
RewriteEngine On
RewriteBase /
# MY RULES
RewriteRule ^/bag$ /index.php?route=checkout/cart
# START OPENCART
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
I tried adding the [L] after the custom URL but it still won't display /bag
I should be able to do
/bag which should loading /index.php?route=checkout/cart
And I should be able to follow the rules below which are the normal OpenCart rules. /bag is giving me 404 however.
The documentation of the rewriting module explicitly states that RewriteRules operate on relative paths, when implemented in dynamic configuration files (".htaccess") as oposed to absolute path when implemented in the real http server's host configuration. That means that you need to change your rules matching pattern. Such rule can actually be implemented in a generic patter that will work in both situation which makes the implementation reusable.
Also you need to terminate the rewriting process when that rule gets applied. Otherwise the following rules will again rewrite the result of your own rule which is not what you want.
RewriteRule ^/?bag$ /index.php?route=checkout/cart [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 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).

Simple .htaccess redirect within the same domain

A bit of a newbie question, how can I redirect a URL like this one: http://example.com.ar to http://example.com.ar/pictures/ using .htaccess ?
or would it be easier to make a rule that searches the entire request URL and adds a /pictures/ when not found?
I have tried:
RewriteBase /
RewriteCond %{HTTP_HOST} example.com.ar [NC]
RewriteRule ^(.*)$ http://www.example.com.ar/pictures/ [R=301,NC]
But it didn't work :/
Sounds pretty straight forward. You would have found hundreds of existing examples...
RewriteEngine on
RewriteRule ^/?$ /pictures/ [R=301]
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 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 htaccess redirect from index.php to / only in specific folder?

I want to create a redirect in my htaccess file. From URL with index.php to page without index.php, but only for one specific folder "buy-new-cars".
For example:
from example.com/buy-new-cars/index.php to example.com/buy-new-cars/
I try to add those lines.But this didn't work.
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(buy-new-cars/.+)index\.php$ /$1 [L,R=302,NC,NE]
Your issue is the RewriteCond you use which does not make sense.
Here is a version with some additional modifications:
RewriteEngine on
RewriteRule ^/?buy-new-cars/(.+)/index\.php$ /buy-new-cars/$1 [R=302,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 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).

Need to redirect subdomain that has the url as parameter

I'm wanting to redirect
http://m.example.com/?url=http://www.example.com/directory/page.html
to
http://www.example.com/directory/page.html (always the value of 'url=')
The are mobile versions of my site that I want to serve on my main site. Right now the search results contain my mobile pages. Until the main, responsive pages are crawled I want to force them to be viewed.
As mentioned in the comments to the question your incoming url is invalid. The query parameter (the value) would have to be url encoded like that to be valid: http://m.example.com/?url=http%3A%2F%2Fwww.example.com%2Fdir‌​ectory%2Fpage.html. Though probably that invalid URL will work in most cases.
This should do what you are looking for:
RewriteMap unescape int:unescape
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(.*)url=http(s?)%3A%2F%2F([^%]+)%2F([^&]+)(&?.*)$
RewriteRule ^ http%2://%3/${unescape:%4}?%1%5 [R=301,END]
Note that the RewriteMap directive can only be used inside the http servers host configuration, not in dynamic configuration files. If you need to use such a file (see general note below), you have to split the above into two separate sections:
Inside your http servers (virtual) host configuration:
RewriteMap unescape int:unescape
Inside your dynamic configuration file:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^(.*)url=http(s?)%3A%2F%2F([^%]+)%2F([^&]+)(&?.*)$
RewriteRule ^ http%2://%3/${unescape:%4}?%1%5 [R=301,END]
If you do not want any http GET parameters to be handed over to the final redirection target, then the above can be simplified:
RewriteMap unescape int:unescape
RewriteEngine on
RewriteCond %{QUERY_STRING} url=http(s?)%3A%2F%2F([^%]+)%2F([^&]+)
RewriteRule ^ http%1://%2/${unescape:%3} [R=301,END,QSD]
And a general hint: you should always prefer to place such rules inside the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

Resources