Problem with htaccess redirect with rewrite module - .htaccess

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

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.

Redirects not working as expected

I have an .htaccess file with several lines. It does not work as expected. Mod_rewrite is enabled. RewriteLogLevel is set to 9.
The first two rules are there to forbid uris with a length more then 80 characters:
RewriteCond %{REQUEST_URI} ^.{80}
RewriteRule .* - [F]
It does not seem to get evaluated as every test url passes through and it does not generate an error either.
I also tried:
RewriteRule .{80} - [F]
But that did not do the trick either. The process ends with a 404, not a 403.
This next rule is not working either. It used to work.
RewriteRule ^(\/)?([\w]+)$ /index.php [L]
The URI /Contact was always handled by this index.php.
Whatever URL I type I get a 404. I should get a 403 or a 200. Not a 404. What am I missing?
Apache has on all directories the permission to read, write and execute and on all files the permission to read and write.
The two urls for testing are:
127.0.0.4/asssssssssssssssssssssssssssssssssssssssssssssssssssssssssssssddddddddddddddddddddd?p=s&s=psv
and
127.0.0.4/Contact
The alias for 127.0.0.4 used is considerate.lb.
Try this rule instead:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+\S{80}
RewriteRule ^ - [F]
Using THE_REQUEST instead of REQUEST_URI as that variable might get overwritten due to presence of other rules in your .htaccess
Finally I have found a solution. The problem was not in the coding of the .htaccess. I replaced the file with a previous version, added the new lines to test the request and it worked all fine.
It is not a satisfactory solution, because it can happen again and I do not have any clue what caused the error. If someone knows the error, I would love to hear what might have been the exact cause and how to solve that properly. I would like to change the tags of the question as the current tags might be misleading (although other people might experience the same problem how apache handles a .htaccess file), but I do not know which tags I should use.

Htaccess - Detecting the URL

For my family members I was giving each person their own subdomain
(sister1.mydomain.com, sister2.mydomain.com, etc...)
I was using PHP to detect the domain, and then I'd load information related to the subdomain dynamically.
I'd like to get rid of the subdomains and use the power of .htaccess
My goal is to give the same URL:
www.mydomain.com/sister1
www.mydomain.com/sister2
www.mydomain.com/mommy
www.mydomain.com/daddyo
Obviously, I don't plan to have literal working directories for each person.
I'd pass the "sister1" portion to a process.php script that takes care of the rest.
I've figure out how to do it by manually typing each RewriteRule in my htaccess file:
Options +FollowSymLinks
AddDefaultCharset UTF-8
RewriteEngine on
RewriteBase /
RewriteRule ^/?sister1$ process.php?entity=sister1 [L]
RewriteRule ^/?sister2$ process.php?entity=sister2[L]
RewriteRule ^/?mommy$ process.php?entity=mommy[L]
RewriteRule ^/?daddyo$ process.php?entity=daddyo[L]
I feel this is the long way of doing it.
Is there a more universal way of extracting the text after the first "/" forwardslash, and passing it to process.php?entity=$1 ?
I tried it this way:
RewriteRule ^/([A-Za-z0-9-]+)/?$ process.php?entity=$1 [NC,L]
I'm getting the apache 404 error: "Not Found".
It is because you have a mandatory / in the beginning of your rule, i.e., you are always looking for something like /sibling in the URL. Your first examples have that first forward slash as optional due to the question mark after it.
You do not need a beginning forward slash - normally the rewrite rule picks up stuff after the domain name
www.example.com/string/mod/rewrite/gets/is.here
So just remove the starting slash and it should work.

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

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]

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'.

Resources