Apache RewriteRule - apache

I need to rewrite the following URL:
http://example.com/folder/subfolder/filename
to
http://example.com/folder/subfolder/filename.txt
I've tried the following:
RewriteRule ^/folder/subfolder/(.*)(|/)$ /folder/subfolder/$1.txt [R=302,L]
However this makes to rewrites go into a loop so I will get:
/folder/subfolder/filename.txt.txt.txt.txt (etc)
Any idea how to make my rewrite rule work?

You need a rewrite condition:
RewriteCond %{REQUEST_URI} !\.txt$
before the rule. I cannot test this at this very moment, but I'm sure, this is what you need.

Related

.htaccess rewrite urls with parameters

I am trying to rewrite my urls through a .htaccess file to make them more clean looking. I have
http://localhost:801/Test/test.php?school=19&name=Greenhaven-Elementary
and it needs to end up looking like
http://localhost:801/Test/test.php/19/Greenhaven-Elementary
In my .htaccess file I have the following
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-/+]+)([0-9]+)$ test.php?school=/$1&name=$2/ [L]
I have tried other ways but being new at using .htaccess files I haven't been able to figure it out.
This should do what you're after:
RewriteEngine On
RewriteCond %{QUERY_STRING} school=(.+)&name=(.+) [NC]
RewriteRule ^(.*)$ http://localhost:801/Test/test.php/%1/%2? [R=301,NC,L]
So what does the above do?
First, it will take the query school= and name= as a condition, if this condition is met then it will grab any version of the variables using (.+).
It will then rewrite the URL using 301 redirection to show http://localhost:801/Test/test.php/anything/anything2. The use of %1 and %2 is to grab the variables from school= / name= and then we use ? to stop the original query string from appearing on the end of the newly rewritten URL.
Make sure you clear your cache before testing this.
EDIT:
I wrote this for the singular query:
RewriteEngine On
RewriteCond %{QUERY_STRING} item=(.+) [NC]
RewriteRule ^(.*)$ http://localhost:801/Test/%1? [R=301,NC,L]
This includes removing test.php and on my server works without issue and returns http://localhost:801/Test/anything

How to create an htaccess rewrite rule with part of parameter as identifier

I need some help with a rewrite rule I am struggling with.
I have the existing rule below, which works well and redirects as follows:
www.site.com/page.php?type=1&category=2&cond=3
redirected to
www.site.com/1/2/3/
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/$ /page.php?type=$1&category=$2&cond=$3 [L]
Now I need to create one new rule that will not interfere with the rule above.
So this url:
www.site.com/page.php?type=1&category=2&page=page-1
should redirect to:
www.site.com/1/2/page-1/
Obviously, the browser should make somehow the difference between www.site.com/1/2/3/ and www.site.com/1/2/page-1/.
This difference can be the part of the third parameter which will be always constant: page- .
I know I need to somehow modify this part of the new rule ([^/]*) but nothing I have tried so far does to job.
Any suggestions how should I accomplish this?
Have these 2 rules in this order:
RewriteRule ^([^/]+)/([^/]+)/(page-[^/]+)/?$ /page.php?type=$1&category=$2&cond=$3 [L,QSA,NC]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /page.php?type=$1&category=$2&cond=$3 [L,QSA]
You should be able to do your rules this way.
RewriteRule ^([^/]*)/([^/]*)/page-([^/]*)/?$ /page.php?type=$1&category=$2&page=page-$3 [L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/?$ /page.php?type=$1&category=$2&cond=$3 [L]

Rewrite rule not applying

I'm really struggling getting to match this URL my htaccess rule and nothing I try seems to work.
This is the url I want to match
http://www.xyz.com/events/my-event-in-town/
RewriteRule ^my-event-in-([^/]+)/$ /new-url/$1/ [R=301]
Hope someone can point out my obvious mistake
I also need to remove the first /events/ part from the URL
It should be:
RewriteRule ^events/my-event-in-([^/]+)/$ /new-url/$1/ [R=301]
Or:
RewriteCond %{REQUEST_URI} ^/events/
RewriteRule my-event-in-([^/]+)/$ /new-url/$1/ [R=301]
Your rule will only match to http://www.xyz.com/my-event-in-town/ because the ^ meant the start of line anchor.

Apache Rewrite problem

http://www.a.com/content/1
http://www.a.com/content/seo-text-1
RewriteRule ^content/(\d+)|.+?-(\d+) action/index.php?id=$2
This rule don't work second url, How to fix it?
Thank you
If for any reason you need to have it in single rule, you can use this:
RewriteRule ^content/(.*)-?(\d+)$ action/index.php?id=$2 [R]
But more readable solution is to use two rules:
RewriteRule ^content/(\d+)$ action/index.php?id=$1 [R]
RewriteRule ^content/.*-(\d+)$ action/index.php?id=$1 [R]

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?
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]
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]
There seems to be an = missing from clops answer to give..
RewriteRule ^feedback/?$ index.php?m=feedback&c=%{QUERY_STRING} [NC,L]
.. at least I need one to make it work.