Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache rewrite rule with parameters?

I have the following URL:

http://domain.com/index.php?m=feedback&cSubject=My Subject

I want to have a rewrite rule so that the following:

http://domain.com/feedback?Subject=My Subject

maps to the previous url. Heres my rule at the moment:

RewriteRule ^feedback?Subject=(.*)$ index.php?m=feedback&cSubject=$1

Doesn't seem to be working tho! Any ideas?

like image 635
James Avatar asked Oct 09 '09 23:10

James


People also ask

How do I rewrite an Apache rule?

A rewrite rule can be invoked in httpd. conf or in . htaccess . The path generated by a rewrite rule can include a query string, or can lead to internal sub-processing, external request redirection, or internal proxy throughput.

What is PT in rewrite rule?

PT|passthrough The target (or substitution string) in a RewriteRule is assumed to be a file path, by default. The use of the [PT] flag causes it to be treated as a URI instead.

What is $1 in Apache rewrite rule?

In your rewrite, the ^ signifies the start of the string, the (. *) says to match anything, and the $ signifies the end of the string. So, basically, it's saying grab everything from the start to the end of the string and assign that value to $1.

What does IfModule mod_rewrite C mean?

The <IfModule mod_rewrite. c>... </IfModule> block ensures that everything contained within that block is taken only into account if the mod_rewrite module is loaded. Otherwise you will either face a server error or all requests for URL rewriting will be ignored.


2 Answers

Query Strings are not parsed by Apache Mod_Rewrite, but there is a workaround. Try this

RewriteRule ^feedback/?$ index.php?m=feedback&c%{QUERY_STRING} [NC,L]
like image 68
clops Avatar answered Nov 13 '22 14:11

clops


You can use RewriteCond statement to do exactly what you want:

RewriteEngine On

RewriteCond %{QUERY_STRING} Subject=(.*)
RewriteRule ^feedback$ index.php?m=feedback&cSubject=%1 [L]
like image 34
Kepi Avatar answered Nov 13 '22 14:11

Kepi