Rewrite URL with .htaccess for 2 parameters - .htaccess

How can I convert URL from:
http://example.com/site/delete-page.php?site=first&page=second
To:
http://example.com/site/first/delete-page/second
Thank You!

This is the normal rule for that specific task:
RewriteEngine on
RewriteRule ^/site/([^/]+)/delete-page/(.+)$ /site/delete-page.php?site=$1&page=$2 [L]
To use the same rule inside a .htaccess style file you have to modify it slightly: remove the leading slash (/) from pattern and target:
RewriteEngine on
RewriteRule ^site/([^/]+)/delete-page/(.+)$ site/delete-page.php?site=$1&page=$2 [L]
This assumes that the .htaccess style file is located inside the folder holding the site folder. You can also place the file inside that site folder, then obviously you have to remove the site/ part from the rules pattern and target. Also you have to take care that the interpretation of .htaccess style files is enabled at all for that host and location.
In general you should always prefer the first version and place such rules inside your http servers host configuration. .htaccess style files are notoriously error prone, hard to debug and they really slow the server down. They are only offered as a last option for situations where you do not have access to the server configuration. For example when using a really cheap shared web hoster service...

Related

How to rewrite rule based on folder and file type in htaccess?

I have been trying to implement a rewrite rule for a downloads folder so that I can serve files that end with .gif, .jpg, .png, .css, .js or .swf and send users to user.php for every other file.
For example: I should hit this URL : www.somewhere.com/downloads/mypic.jpg,
but when I try : www.somewhere.com/downloads/my.pdf I should be redirected to user.php.
So far, I have :
RewriteRule ^*/downloads//!(.*)\.(gif|jpg|png|jpeg|css|js|swf)$ /base/user.php?a=$1 [R=302,L]
Here are some samples for expected behaviour :
good
www.somewhere.com/downloads/mypic.jpg
www.somewhere.com/downloads/otherpic.png
www.somewhere.com/downloads/scripts/jquery.js
bad
www.somewhere.com/downloads/my.pdf > send the request to www.somewhere.com/base/user.php
www.somewhere.com/downloads/readme.txt > send the request to www.somewhere.com/base/user.php
www.somewhere.com/downloads/postman.json > send the request to www.somewhere.com/base/user.php
This probably is what you are looking for:
RewriteEngine on
RewriteRule ^/?downloads/([^/]+)\.(gif|jpg|png|jpeg|css|js|swf)$ /base/user.php?a=$1 [R=302]
The above rule will redirect the browser, so change the visible URL. That is what you suggest yourself in your question. In case you want to implement an internal rewriting instead you need to alter the flag slightly:
RewriteEngine on
RewriteRule ^/?downloads/([^/]+)\.(gif|jpg|png|jpeg|css|js|swf)$ /base/user.php?a=$1 [END]
This rule will work in the http servers host configuration and likewise in dynamic configuration files (.htaccess). Obviously the rewriting modules must be enabled in your http server. If you decide to use a dynamic configuration you need to enable its interpretation first, take a look at the AllowOverride directive in the official documentation for that.
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 supported 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).
In your modification of the question it becomes clear that what you try to implement actually is the opposite of what you apparently asked before. Here is a modified version of the above rule:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/downloads/[^/]+\.(gif|jpg|png|jpeg|css|js|swf)$
RewriteRule !^/?downloads/([^/]+\.\w+)$ /base/user.php?a=$1 [R=302]
And another general remark: often it makes more sense to not grant any access directly to files in the server side physical file system but to implement a router script instead which controls access to all such files. This allows for more fine grained access control and keeps the physical layout separated from the URL set you define.
Have this rule inside downloads/.htaccess file (create it if it doesn't exist):
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(gif|png|jpe?g|css|js|swf)$ [NC]
RewriteRule .* /base/user.php?a=$0 [R=302,L,NC,QSA]
I decided to redirect, when a request to a forbidden extension exists. This worked for me :
RewriteRule ^/downloads/(..(pdf|txt|json))$ /base/user.php?a=$0 [R=302,L]

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

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]

Clean URL rewriting rule

right now my url looks like this:
http://domain.com/en/c/product%2C-product2%2C-product3/82
where last number is category numer.
And im trying to rewrite it and redirect user to url which should look this one:
http://domain.com/82/product-product2-product3
The clue is I want to hide "en/c/" part and clean url from commas and blank spaces. I'm completely green in rewriting.
Any ideas?
You can use these 2 rules in your root .htaccess for that:
RewriteEngine On
RewriteBase /
RewriteRule ^en/c/([^,\s]*)[,\s]+([^,\s]*)/(\d+)/?$ $3/$1$2 [NC,L,NE,R=302]
RewriteRule ^(en/c)/([^,\s]*)[,\s]+(.*)/(\d+)/?$ $1/$2$3/$4 [NC,L]
In order for this to work, we need to tell the server to internally redirect all requests for the URL "url1" to "url2.php". We want this to happen internally, because we don't want the URL in the browser's address bar to change.
To accomplish this, we need to first create a text document called ".htaccess" to contain our rules. It must be named exactly that (not ".htaccess.txt" or "rules.htaccess"). This would be placed in the root directory of the server (the same folder as "url2.php" in our example). There may already be an .htaccess file there, in which case we should edit that rather than overwrite it.
The .htaccess file is a configuration file for the server. If there are errors in the file, the server will display an error message (usually with an error code of "500").
If you are transferring the file to the server using FTP, you must make sure it is transferred using the ASCII mode, rather than BINARY. We use this file to perform 2 simple tasks in this instance - first, to tell Apache to turn on the rewrite engine, and second, to tell apache what rewriting rule we want it to use. We need to add the following to the file:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^url1/?$ url2.php [NC,L] # Handle requests for "url1"

Can one include separate files in htaccess for a list of redirects?

My main htaccess file does a bunch of things for my site to function correctly.
I have added redirects for pages that have moved. I don't have root access to the server and using .htaccess is my only option.
Is it possible to include separate files for the redirects in the .htaccess file so I can keep them separate and write programatically to the additional files that hold my redirects?
Basically I want to reference separate files from my .htaccess to manage rules dynamically and also neaten up one long .htaccess file with a few smaller files.
I also want to add redirect rules on the fly as things change on the site within my application.
You can use a RewriteMap http://httpd.apache.org/docs/2.3/rewrite/rewritemap.html
Let's say your map file looks like this and is called moved.map:-
/about profile
/page/that/has/moved new/location
You .htaccess would need something like this:-
RewriteMap moved txt:moved.map
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond ${moved:%1|NOT_PRESENT} !NOT_PRESENT [NC]
RewriteRule .? ${moved:%1} [NC,R=301]
This will redirect with a 301 status code http://your.domain.com/about to http://your.domain.com/profile and redirect http://your.domain.com/page/that/has/moved to http://your.domain.com/new/location
You can then programmatically create moved.map.
I hope that helps.
If you are using .htaccess files then don't bother with RewriteMap -- it only applies if you have root access to the server or vhost config, which is never the case when you purchase a shared service offering.
If you are constrained to use .htaccess files then you have two options:
The first is to do what some packages do and that is to get your application to rewrite the .htaccess file based on a rewrite map that you maintain within in it. The best way to do this is to have "bookends" in your .htaccess file e.g.
##++AUTOMATIC rewrite rules
<rules inserted by your app>
##--AUTOMATIC rewrite rules
And when an update occurs have your app read in the .htaccess, swap out the section between ##(++|--)AUTOMATIC rewrite rules, write it back to a temp file, then move the temp file to .htaccess (this makes the rewrtie-back atomic on *nix OSs).
The second which might work if you know some regexp regular pattern which covers the rewrites (this is often the case) then use a rule to map them to a redirector script which looks up the new target and itself issues a:
$server = $_SERVER['HTTP_HOST'];
header( "Location: http://$server/$newTarget?$parameters", TRUE, 301 );
Note the 301 redirect -- this means that client browsers should cache this and remember this in future.

RewriteRule from subfolder path to another subfolder path

I'm currently creating a bunch for RewriteRules for a customer migrating their website to a new platform and having a weird issue with the following rule:
RewriteRule ^/folder/filename.cfm /another_folder/subfolder/filename.cfm?name=value
I've never had problems with the right side of the rule, which BTW the URL does work. But the left side is my first with a folder.
So what happens is I get as a 404 error. But the following rule will work just fine.
RewriteRule ^filename.cfm /another_folder/subfolder/filename.cfm?name=value
Anyone have any ideas. BTW, were this is on a Windows system using Helicon Ape Manager.
I guess you are using .htaccess file inside /folder/? Here is quote from Helicon Ape documentation Context and processing order :
Remember when RewriteRule directive is used inside .htaccess configuration files it will automatically strip the local directory prefix from the path and apply rules only to the remainder. You can use RewriteBase directive to explicitly specify a base path for the rules.
The issue I discovered after using an HTTP request sniffer is that APE will normalize the redirecting links. So a link that includes %2E will get escaped to %372E. And so the links were becoming invalid.

Resources