Apache Rewrite problem - apache

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]

Related

Rewrite one directory to another .htaccess

I want to automatically redirect http requests to news/images to ../images.
Is that possible with .htaccess?
Thing is: request to www.site.tld/news/images ... should go to www.site.tld/images ...
I have tried:
RewriteEngine On
...
...
RewriteRule (.*)news/images(.*) ../images [R=301,L]
not working.
I have ensured that apache have mod_rewrite.c enabled.
To redirect all requests for /news/images/ to /images/, capture the part after images and use it in the RewriteRule
RewriteRule ^news/images(.*)$ /images$1 [R,L]
When it works as it should, you may replace R with R=301. Never test with R=301.
You can use:
RewriteRule ^www\.site\.tld/news/images$ /www.site.tld/images?&%{QUERY_STRING}
or you can also use:
RewriteCond %{HTTP_HOST} ^www.site.tld/news/images$ [NC]
RewriteRule ^(.*)$ http://www.site.tld/images/$1 [R=301,L]
But as #arkascha said, please do some research first, there are MANY answers to this sort of problem! :) Either way, I hope this helps.

Apache RewriteRule

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.

Getting URL parameters with mod_rewrite

Here is my current rewrite rule
RewriteRule ^page/([^/\.]+)/?$ index.php?page=$1 [L]
RewriteRule ^post/([^/\.]+)/?$ index.php?post=$1 [L]
RewriteRule ^cat/([^/\.]+)/?$ index.php?cat=$1 [L]
three lines to do basically the same thing. Is there a way I can clean this up a bit? Is there one line that will accomplish this?
Also, Is there a way to pull multiple variables from a single url string? So if I had something like:
http://www.mysite.com?page=foo&id=123&color=red
how would I convert it to this:
http://www.mysite.com/page/foo/id/123/color/red
RewriteRule ^(cat|post|page)/([^/\.]+)/?$ index.php?$1=$2 [L]
because jacek_K to you one part, I give you answer of second part :
to rewrite this URL(http://www.mysite.com/page/foo/id/123/color/red), use this:
RewriteRule ^page\/([^/\.]+)\/id\/(\d+)\/color\/([^/\.]+) index.php?page=$1&id=$2&color=$3 [L]

mod_rewrite: rewrite specific URL

I want to rewrite a specific URL, I'll show an example so you'll understand what I mean.
First, my current rewrite rule:
RewriteRule ^/?([a-zA-Z0-9/-]+)/?$ /index.php [NC,L]
Now I want this URL:
http://example.tld/foobar?test
Rewritten to:
http://example.tld/foobar
Note: only for /foobar?test! E.g. not for /somethingelse?test and also not for /foobar?blah!
Thanks in advance!
EDIT: I realized I want a 301 redirect from /foobar?test to /foobar, not a "traditional" rewrite. Hope that is possible.
RewriteCond %{QUERY_STRING} ^test$
RewriteRule ^/foobar$ /foobar [NC,R=301,L]

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.