Apache .htaccess redirect URL directories containing parameters - apache

Sorry to be the asker of yet another tedious mod_rewrite question but after having made no progress in the last few hours, I thought it was time to ask ;)
I am trying to redirect URLs like these:
/some/thing?a=1 --> http://something-else.com/blah
/some/thing?a=1&b=whatever --> http://something-else.com/blah2
No need to keep the param values - the new URL will be hard-coded for each one I have to be redirected.
Have tried a few different things from other posts but with no joy so I am back to square one so any suggestions would be most welcome.
Thanks! :)

You can use the following rule-set:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^a=1$
RewriteRule /some/thing http://something-else.com/blah [L]
This is indeed quite a common question, and people tend to overlook the QUERY_STRING variable. Have you tried it before?

This is what I used in the end:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(a=1&b=whatever)$
RewriteRule /some/thing http://something-else.com/blah2 [L,R=301]
I was adding the rules at the bottom rather than directly below the "RewriteEngine On" which was preventing it from working.
This solution does still append the params to http://something-else.com/blah2 which isn't exactly what I wanted but it will do.

Related

Block direct access to .php files, less the index.php file and ajax.php file

been looking for your help, i found a method, but it is not as i wish. if someone can help me.
What I want is that nobody can enter a direct URL with .php
example when I enter my domain.com/buy/product.php, I want it to be forbidden,
I was looking for information here, I found this code that worked for me but in .htaccess
RewriteEngine On
RewriteCond %{THE_REQUEST} "^.+? [^?]+\.php(?:[?/# ]|$)" [NC]
RewriteRule !^index\.php$ - [F,L,NC]
it worked fine for me, but the problem that I in a directory /include/ajax.php , I use an ajax. and it gives me error to execute the ajax by browsing.
Now what I'm thinking how to make it work with that htaccess code that you can enter the index.php and /include/ajax.php, I tried all means but it does not work for me.
In another case if you know any code to add to my php or how to do for my version which is version 7.3, but without ruining my code.
Rather than giving you the answer straight out, I'm going to give you some hints so that you aren't copying code you don't understand.
Each RewriteRule has three parts:
the pattern to match against the URL sent by the browser
the URL to rewrite to
an optional set of flags for extra options
Before each rule, you can optionally have one or more RewriteCond lines which apply extra conditions to the rule; each has three parts:
a variable to match against
the pattern to match
an optional set of flags for extra options
The most important flag in this case is [F], short for [forbidden], which says "if the rule matches, instead of rewriting or redirecting, just server a 403 response.
You should very rarely need to test against %{THE_REQUEST}, which is a raw version of the request line from the browser; much more often, you want %{REQUEST_URI} and/or %{QUERY_STRING}.
The patterns in both RewriteRule and RewriteCond can be negated (i.e. "must not match this pattern") by starting them with !
So, if you wanted to return a 403 for all URLs ending ".bad", except for URLs ending "not.bad" or "only-a-little.bad", you could write this (note that $ is the way to say "must end here" in the regex patterns):
RewriteCond %{REQUEST_URI} !not.bad$
RewriteCond %{REQUEST_URI} !only-a-little.bad$
RewriteRule .bad$ - [F]
Hopefully it should be straight-forward enough to see how to adapt that to your requirements.
The full list of options and variables available is in the Apache manual.
After 2 days of looking for some code, I was able to read and understand.
study how htaccess works.
Thanks to the users who guided me, I found the solution.
Although my title is not quite correct.
My intention was always to block all .php that always the user wanted to enter directly by .PHP, I had found the code above, but it did not work with a specific file in the /include/ajax.php folder, exactly it was an ajax, I could not find solution.
exactly it was an Ajax, I could not find the solution to make it work.
Until I managed to solve this way.
RewriteEngine on
RewriteCond %{THE_REQUEST} ajax\.php [NC]
RewriteRule ^ - [NC,L]
RewriteCond %{THE_REQUEST} .+\.php [NC]
RewriteRule ^ - [F,L]
This causes all .php to be blocked, except the index.php and the /include/ajax.php file.
This is how it worked for me.
If I am right or wrong, can you give me some guidance.
I leave this in case someone might find it useful in the future.
I was always recommended to route my php, that I would forget about these problems.
I will keep it in mind as I move forward in the future, to route my php.

Apache Mod_Rewrite Htaccess for Dynamic URL

I have this link
www.example.com/1/title
which goes to
www.example.com/post.php?author=1&title=title
because of this rule
RewriteRule ^post/([0-9]+)/([\w-_:]+)/?$ post.php?author=$1&title=$2 [L,QSA,NC]
Fine, but now how do make it such that if somebody types in www.example.com/post.php?author=1&title=title to redirect to www.example.com/1/title
I have spent literally hours online researching this but the information is vague (at least for me) and not working.
2 things stump me so far:
Writing the pretty url into dynamic and then dynamic into pretty -
Doesn't that create a loop?
I also wanted to go the route of a 301 redirect but I could not find
any workable code that take variables from the first link to put into
the redirect. In my head a 301 would be the right choice, but I see a
lot of people (examples) doing it through RewriteRule.
I understand that (groups) can later be accessed by using $1 and $2... but when trying the reverse I cannot make it work. Eg:
RewriteRule ^post.php?author=([0-9]+)&title=([\w-_:]+)$ post/$1/$2
But like I said nothing works. I've been beating my head on sites like http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html but I cannot fully understand (or apply) what I'm reading there. Can you please let me know what I'm doing wrong or how I should approach this problem?
Many thanks for any help you can give me
You need a new rule like this:
RewriteCond %{THE_REQUEST} \s/+post\.php\?author=([^&]*)&title=([^\s&]+) [NC]
RewriteRule ^ post/%1/%2? [R=302,L]
RewriteRule ^post/([0-9]+)/([\w-_:]+)/?$ post.php?author=$1&title=$2 [L,QSA,NC]

Writing a RewriteRule with just one exception

For a project I need to write a RewriteRule in a .htaccess file but unfortunately I have nearly no experience how to write such rules. What I want to do is rather simple: A complete redirect from one path to another with just one exception. Let me show you my try:
RewriteCond %{REQUEST_URI} !^/success$
RewriteRule ^.*/wop_de/checkout/onepage.*$ http://%{HTTP_HOST}/wop_de/onestepcheckout/ [R=301,L]
What I have thought: If there is no string like "/success" in the URL do a redirect from "/wop_de/checkout/onepage/" to "/wop_de/onestepcheckout/". Well, I guess I was thinking the wrong way as it doesn't work. Could you help me, please?
Also do you know a good tutorial to learn how to write such rules? Thank you in advance!
Use this rule:
RewriteRule ^wop_de/checkout/onepage.*$ /wop_de/onestepcheckout/ [R=301,L]
.htaccess is per directory directive and Apache strips the current directory path (leading slash) from RewriteRule URI pattern.

.htaccess mod_rewrite - Non-matching patterns

I'm having trouble getting non-matching (negation) mod_rewrite patterns to work.
In the example below I want all requests redirected to "/promo-page", however, don't allow a redirect loop (hence the RewriteCond).
### Request URL: http://example.com/promo-page ###
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_URI} !/promo-page
RewriteRule .* /promo-page [R=302,L]
When a request for "/promo-page" is made, the RewriteCond rule is ignored and a redirect loop occurs.
Any ideas why the Exclamation Mark (!) isn't working? Is the syntax incorrect?
Thanks in advance.
UPDATE
I've also tried these rules in an online mod_rewrite tester and they also don't work (if you use "home" as the URL and "/home" as the REQUEST_URI).
http://martinmelin.se/rewrite-rule-tester/
Remove RewriteCond %{REQUEST_URI} !^/$ - it will always be true, as Jon says.
Thanks all for your answers and thoughts. I finally tracked down the issue, so here's the details in the hope it may help someone else down the track.
What was confusing the situation was more rules further down in the .htaccess file. These weren't posted in my original question as I didn't believe they were relevant and would even affect the outcome.
These additional rules re-write the Clean URL's into a GET request. I was under the (false) impression that %{REQUEST_URI} also includes the GET variables tacked onto the end of URL's. It doesn't. You must use %{QUERY_STRING} to separately check the content of the GET query string.
Mod-Rewrite Logging was very useful in helping me debug this issue. I wasn't able to enable it on our live/production cPanel server, so I installed XAMPP on my PC, enabled logging and played with the rewrite rules locally until I got it working.

.htaccess RewriteRule using # sign?

I am trying to have a URL that would look like:
mysite.com/#username
Which would route to something like:
subdomain.mysite.com/user/#username
I'm terrible with RewriteRules and have been struggling to try to get this to work. Some of the things I have tried are:
RewriteRule subdomain.mysite.com/user/(.*) mysite.com/$1 [R=302,NC,L]
RewriteRule ^(.#)(.*) subdomain.mysite.com/user/$1 [R=302,NC]
RewriteRule ^(.#)([A-Za-z0-9_]+) mysite.com/user/$1 [R=302,NC]
I realize those probably make absolutely no sense. Every time I try to get my head around how the routing works, I get turned around and start writing crap like you see above.
Any pointers would be appreciated. Thanks.
You can't route to a subdomain (using htaccess anyway) but you can redirect there.
RewriteEngine On
RewriteBase /
RewriteRule #(.*) http://subdomain.mysite.com/user/#$1 [L,R=301]
The # symbol should be okay https://stackoverflow.com/a/1547940/763468