Trying to redirect from domain.com/user/12345 to domain.com/user?id=12345 - .htaccess

I'm trying to redirect from domain.com/user/12345 to domain.com/user?id=12345 and from domain.com/user/12345/profile to domain.com/user/profile?id=12345.
I've written this rule in .htaccess:
RewriteRule ^user/([0-9]*)(/.*)?$ user$2?id=$1 [L,QSA]
It works ok for domain.com/user/12345/profile but domain.com/user/12345 is not redirected.
I've also tried with this simplest form for this case:
RewriteRule ^user/([0-9]*)$ user?id=$1 [L,QSA]
I've tested both in http://htaccess.madewithlove.be/ and seems to work fine.
¿What is my mistake? ¿How can I do it?
UPDATE:
The next rule I've in this file rewrites from domain.com/user to domain.com/user.php and so on. If the previous rule is not defined it rewrites ok from domain.com/user/12345 to domain.com/user/12345.php but with the previous rule defined it neither does this rewriting.
Does it means that there is any kind or transformation that skip the second rule or that there is any kind of misyake that stops rules verification?

I've found the solution. I don't know why it worked well in a case and not in the other but I'd forgotten the slash so it was trying to rewrite to user?id=12345and not to /user?id=12345.
So the rule must be like this one:
RewriteRule ^user/([0-9]*)(/.*)?$ /user$2?id=$1 [L,QSA]

Related

I am not able 301 redirect domain.tld/?cur=usd to domain.tld

I try to redirect domain.tld/?cur=usd to domain.tld (there are many curencies, this is only example of one currency - we do not use anymore this solution).
I need to redirect only home with parameter to home without parameter. The other urls worked for me, I'm just having trouble getting work with that one.
I try to search and use online generators but none of the solutions work.
Here is what I am trying:
RewriteCond %{QUERY_STRING} (^|&)cur\=(.*)($|&)
RewriteRule ^$ /? [L,R=301]
// update
before this rule I have only
#bof redirects
RewriteEngine enabled
...and then there are redirects for other URLs, but I tested this rule separately first and the result was the same...
It not redirect me.
Thanks for the help and maybe an explanation of what I'm doing wrong.
RewriteCond %{QUERY_STRING} (^|&)cur\=(.*)($|&)
RewriteRule ^$ /? [L,R=301]
As mentioned in comments, this should already do as you require, providing there are no conflicts with other directives in the .htaccess file.
However, the regex in the preceding condition is excessively verbose for what you are trying to achieve (ie. just testing for the presence of the cur URL parameter).
If you simply want to check for the cur URL parameter anywhere in the query string then the regex (^|&)cur= would suffice (and is more efficient). No need to backslash-escape the literal =. And if the URL parameter always appears at the start of the query string then just use ^cur=.
I found the problem - it was something with the hosting, after a reboot everything started working as expected.
So I can confirm that this rule is fine.
Sorry for question.

Pattern Matching htaccess conflict

I have migrated my site to new software and am trying to ensure that older links are appropriately redirected to the new url's. I thought I had it working until I received the message from Google regarding increasing 404's.
I seem though to be causing a conflict between the new htaccess requirements and my changes to address the old links.
So the new links look like this:
http://www.exampledomain.com/search/?q=searchterm
And in the htaccess they are picked up like this:
RewriteRule ^search/(.*)$ search.php?q=$1
The above is working as it should.
The old links can look like either of these:
http://www.exampledomain.com/search/searchterm/
or
http://www.exampledomain.com/search/searchterm
I had put this in to the htaccess
RewriteRule ^search/(.*)/$ http://www.exampledomain.com/search.php?q=$1 [R=301,NC,L]
If I don't add the first rule then the new url's just bring up 404's.
If I add second rule the it stops the searchterm being passed and conflicts with the first rule.
Have tried a few things but think there must be an issue with the matching or something else i'm missing.
Any ideas appreciated.
** Added **
So after the first reply I made the change as suggested and it caused a couple of issues but switching the order of the rules has fixed that but has not quite fixed the issue
So now I have this:
RewriteRule ^search/(.*)/?$ http://www.exampledomain.com/search.php?q=$1 [R=301,NC,L]
RewriteRule ^search/(.*)$ search.php?q=$1
The above works for these url's now:
http://www.exampledomain.com/search/?q=searchterm
and
http://www.exampledomain.com/search/searchterm
But for url's like this:
http://www.exampledomain.com/search/searchterm/
it results in this with a trailing slash which prevents the search:
http://www.exampledomain.com/search.php?q=drama/
So just need to remove or not have the trailing slash
I think you are just missing a ? in the rule since the / is optional:
RewriteRule ^search/(.*)/?$ search.php?q=$1 [R=301,NC,L]
Update to address trailing slash:
I'm guessing that the .* is consuming the trailing / before the next rule. To fix that, we need to exclude it from the match:
RewriteRule ^search/([^/]*)/?$ search.php?q=$1 [R=301,NC,L]
Update to address added case where parameter appears after slash:
I'm not sure if you mean literally /search/q=searchstr or /search/?q=searchstr so I will attempt to address both cases.
If it's the latter, which is a true query string, place this rule above the first using QSA in order to pass the query string along to the new URL:
RewriteRule ^search/$ search.php [R=301,NC,QSA,L]
To address the first variation (without the ? query string), you will need to place this rule above the first, which literally looks for the q=:
RewriteRule ^search/q=(.*)$ search.php?q=$1 [R=301,NC,L]
And since I get the feeling you'll update this question again to ask about what happens if there is a trailing slash, I'll go ahead and modify that rule to handle this case as well:
RewriteRule ^search/q=([^/]*)/?$ search.php?q=$1 [R=301,NC,L]
If these rules still don't solve every case for you, then you're dealing with some really bad code from your previous URL and I feel very sorry for you. :P

rewriteRule for URL path not working correctly

I need help with a URL problem I've encountered with a rewriteRule.
What I need it to do is following: example.com/en/page/page/
At the moment the following works fine: example.com/en/page/
But once it goes like "example.com/en/page/page/" I receive a 404 - page not found error even if the page in fact is located in the serverfiles.
The clue here is that I use a variable in the /en/ part of the URL (multilanguage system) and it seems that I cannot figure out how to get it to work with that included.
At the moment I have the following rewriteRule in my .htaccess file.
RewriteRule ^([^/]*)/([^/]*)/?$ index.php?lang=$1&uri=$2 [L]
Do any of you have a clue on what might work?
Best regards,
PureDarkness
You don't include anything behind the second /. You could try:
RewriteRule ^([^/]*)/(.*)$ index.php?lang=$1&uri=$2 [L]
And you can add [QSA] if you also need to get the parameters.

htaccess Rewrite subdirectory to php page

I've successfully rewritten
RewriteRule ^alphabetical alpha.php
So that when anyone comes to www.example.com/alphabetical it loads alpha.php. However, I'm having problems creating a rule so that when someone comes to www.example.com/alphabetical/a it should loa alpha-a.php. This is the rule I've tried that isn't working:
RewriteRule ^alphabetical/a alpha-a.php
put your second more specific rule on top of the the first rule.
RewriteRule ^alphabetical/a alpha-a.php
RewriteRule ^alphabetical alpha.php
further, you can use a $ at the end to denote an end of url, saying urls ending with 'a'.

Problem with htaccess redirect with rewrite module

I have an old url at:
http://example.com/search/admin
I want to make it go to:
http://example.com/cgi-bin/admin
This is what I have so far, which could be completely wrong...
RewriteRule ^/search/admin$ https://example.com/cgi-bin/admin
The mod_rewrite is definitely on and working and I am using apache 2.2.
Little correction to Benubird post:
RewriteRule ^search\/admin\/?$ cgi-bin/admin [L]
RewriteRules never start with a slash and the redirect can be without slash.
the ^ character matches the start of a line ( or url in this case). Just a thought, but maybe your line should be:
RewriteRule ^/search/admin$ /cgi-bin/admin
Otherwise I suspect you'll either not be redirected, or be getting redirected to https://example.comhttps://example.com/cgi-bin/admin

Resources