mod_rewrite help can't get it to work - .htaccess

This is the first time I use mod_rewrite and I can't get it to work. I have a website with bands and their IDs. What I want:
a URL /bands/My_Band_id13/ should redirect to /bands/index.php?bandname=My_Band&bandID=13
What I have:
RewriteRule ^/bands/(.*)_id(.*)/$ /bands/index.php?bandname=$1&bandID=$2
What am I doing wrong?

try adding the 'qsappend|QSA' (query string append) rewrite flag to your rule, ie.
RewriteRule ^/bands/(.*)_id(.*)/$ /bands/index.php?bandname=$1&bandID=$2 [QSA]
UPDATE: also, try removing / outcommenting your RewriteBase /. if this doesn't work, neither, try moving your .htaccess file into the same directory your index.php is in and adapt the RewriteRule, eg.
RewriteRule ^(.*)_id(.*)/$ index.php?bandname=$1&bandID=$2 [QSA]

Related

mod_rewrite with two parameters

Im getting a headache over this.
If the requested css file has qp defined it should rewrite as line 1. But if it doesn't it should rewrite as line 2.
Why isn't this working?
RewriteRule /css/(.*).css?qp=(.*)$ /build/css.php?request=$1&qp=$2 [QSA]
RewriteRule /css/(.*).css$ /build/css.php?request=$1 [QSA]
Can anyone explain why this isn't working?
Remove leading slash from RewriteRule. It doesn't receive it. And doesn't receive Query string. Ifqp is present, QSA will save it. Must be enough:
RewriteEngine on
RewriteRule ^css/(.*).css$ /build/css.php?request=$1 [QSA]

Need to .htaccess redirect path with a variable URI segmant to another directory without changing URL

My current .htaccess looks like this:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^[0-9]+/pos /pos/$1 [L]
Currently this will redirect a url such as example.com/234234234/pos to example.com/pos
I would like it to load the directory example.com/pos, but without losing the original URL (example.com/234234234/pos) from the address bar.
Basically, the number listed in the url can change, but I always want it to load the same path.
This rule doesn't look right:
RewriteRule ^[0-9]+/pos /pos/$1 [L]
As you're not capturing anything hence there is no $1. If you want to ignore any number before /pos then use this rule:
RewriteRule ^[0-9]+/(pos)/?$ /$1 [L,NC]
Thank you anubhava. Part of your answer helped me. I did want the numbers ignored, but didn't want them to disappear.
This is the line I needed:
RewriteRule ^[0-9]+/pos/ /pos/ [L,NC]
Did not need to capture anything at all. That is what was causing the redirect.

.htaccess 301 redirect path and all child-paths

I want accesses to e.g. www.thisdomain.com/docs/path1/path2 to redirect to www.thatdomain.com/path1/path2
(Note that docs is not a part of the new path)
I have the following on www.thisdomain.com:
RewriteEngine on
RewriteRule ^docs/* http://www.domain.com/ [R=301,L]
If I access www.thisdomain.com/docs, it directs to www.thatdomain.com, but if I access a child-path like www.thisdomain.com/docs/path1/path2 it fails. Is it possible for the redirect to intercept the child-path access and redirect as I need? If so, any pointers?
Thanks.
With regular expressions, * means any number of the previous atom, which will match /docs and /docs/. Try this:
RewriteEngine on
RewriteRule ^docs$ http://www.domain.com/ [R=301,L,QSA]
RewriteRule ^docs/(.*) http://www.domain.com/$1 [R=301,L,QSA]
(QSA is query string append, so /docs/foo?bar=baz won't lose the ?bar=baz.)
According to section "Redirect Old domain to New domain using htaccess redirect" of the first Google result which I found searching for "htaccess redirect" (without the double quotes), this piece of code will suffice:
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
According to their description of the technique, it will redirect the directory the .htaccess file is placed in recursively (including any child paths), just as you intend. Of course, mod_rewrite needs to be present for the rewrite directives to work.

How to remove part of a URL using .htaccess

I have a url like this.
/domains/details.php (NOTE: domains is dynamic and can be anything)
How do I remove the domains part from the URL using .htaccess so the actual lookup is:
/details.php
OR it'll be cool if I can get domains into the URL.
/details.php?page=domains
Thanks!
Scott
RewriteEngine on
RewriteBase /
RewriteRule ^([^/]+)/details.php$ /details.php?page=$1 [R=301]
Leave off the [R=301] if you want an internal redirect rather than an actual HTTP redirect.
To preserve existing query parameters you can change the rule to this:
RewriteRule ^([^/]+)/details.php(.*)$ /details.php?page=$1&%{QUERY_STRING} [R=301]
Please try to use the following rules to deal with your last request:
RewriteRule ^(?!domains/.*)([^/]+)/details.php$ domains/details.php?page=$1 [R=301,QSA]
RewriteRule ^domains/details.php$ details.php [NC,L]

URL rewrite using mod_rewrite in .htaccess

I am trying to rewrite the URL using the mod_rewrite apache module.
I am trying to use it as below :
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^wants/testing.htm wants.php?wantid=$1 [L]
I have created a .htaccess file in the directory from where the files are accessed.
The mod_rewrite is also enabled.
With all these done, i still haven't been able to get it working.
Can someone please help me with this? Please let me know if i am missing something
Thanks in Advance,
Gnanesh
As per OP's comment:
The URL shows
mydomain.com/wants.php?wantid=123. I
need to make it look like
mydomain.com/wants/123
This should work for your case:
RewriteEngine on
RewriteBase /
RewriteRule ^wants/([0-9]+)$ /wants.php?wantid=$1 [L]
It will allow you to use http://yoursite.com/wants/123 and will silently rewrite it to wants.php?wantid=123
I think a leading slash (or rather the lack thereof) might be yoru problem:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^wants/testing.htm /wants.php?wantid=$1 [L]
Otherwise Apache might be looking for the PHP file in /wants/wants.php as it'll be treating it as a relative URL.
Hmm, so I gave it some thought and I guess you could do something like this ( only changed the regexp according to your comment, if I understood correctly ):
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^wants/\d+$ /wants.php?wantid=$1 [L]
But I guess you could also leave out the $1 reference, and still be able to access the id in your wants.php. So
RewriteRule ^wants/\d+$ /wants.php [L]
should work too, and you can then use something like
<?php
$request = split('/', $_SERVER["REQUEST_URI"])[1];
?>
in your wants.php where the last element of the array would be your id ( or anything else you ever decide to rewrite and send to the script ).
If you want to use the rule in the .htaccess file in your wants directory, you have to strip the contextual wants/ from the start of the pattern. So just:
RewriteEngine on
RewriteRule ^([0-9]+)$ /wants.php?wantid=$1 [L]
Otherwise, if you want to use the rule in the .htaccess file in your root directory:
RewriteEngine on
RewriteRule ^wants/([0-9]+)$ wants.php?wantid=$1 [L]

Resources