Htaccess rewrite rule redirected but server not processh - apache

I want my server to read this page example.com/myapp.php?p=11 against the URL example.com/myapp/my-technical-effort.
I have tried this .htaccess rule:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/myapp\.php$
RewriteCond %{QUERY_STRING} ^p=11
RewriteRule ^(.*)$ example.com/myapp/my-technical-effort [QSD,R=301,L]
it redirects to the new URL but the server says "page not found" on this server with 404 error.
Can you please help me to rewrite the rule so the server can serve the page example.com/myapp.php?p=11 against the URL example.com/myapp/my-technical-effort.
My current .htaccess is as follows:
<IfModule mod_headers.c>
Header always append X-Frame-Options ALLOWALL
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
Header always set Content-Security-Policy "upgrade-insecure-requests;"
</IfModule>
Anyone can help me please?

Your implementation is the wrong way round. Have a try that way:
RewriteEngine on
RewriteRule ^/?myapp/my-technical-effort$ /myapp.php?p=11 [QSD,L]
You need to implement your rewrite rules based on actual imcoming requests. Those should get rewritten internally, so those have to be matched.
This asumes that the actual request you want to rewrite is to /myapp/my-technical-effort (your question is a bit vague in that), and that /myapp.php?p=11 is the internal resource you want to get queried.
UPDATE:
Your comment below suggests that you are actually looking for a solution to redirect the "old" URL to the new one. It is indeed possible to combine that with above rewriting:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^p=11$
RewriteRule ^/myapp\.php$ /myapp/my-technical-effort [QSD,R=301,L]
RewriteRule ^/?myapp/my-technical-effort$ /myapp.php?p=11 [QSD,L]
This will redirect any client that requests the "old" url (https://example.com/myapp.php?p=11) to the "new" url (https://example.com//myapp/my-technical-effort). And it will internally rewrite incoming requests to the "new" url to the actual script able to respond to the requests (/myapp.php).

Related

Redirect URL (including port) with params using .htaccess

I am trying to redirect from
http://www.example.com:81/my/api/search?query=test
to
http://www.example.com:81/my/php/api.php?query=test
using
RewriteCond %{QUERY_STRING} ^query=(.*)$
RewriteRule ^api/search(.*)$ /php/api.php?%1 [L]
However, it doesn't work for me. Additionaly for testing htaccess rules I use http://htaccess.madewithlove.be/.
Possibly even better would be to check if request starts from ^api and it's GET type (I guess using RewriteCond %{REQUEST_METHOD}) then redirect to /my/php/api.php?[query params here]
Anyone can point me into right direction?
If your .htaccess is located in /my/ directory then you can use:
RewriteEngine On
RewriteBase /my/
RewriteCond %{QUERY_STRING} ^query=.
RewriteRule ^api/search/?$ php/api.php [L]
QUERY_STRING will be automatically carried over to target URL.

How to use htaccess Rewrite rule for just part of a url

I am trying to create a rewrite rule in my httacess file for part of the url. I want to rewrite the url if the request contains a specific string. For example change the url only if contains /members/
So
mydomain.com/members/
mydomain.com/members/activity/ros1...
mydomain.com/members/ay/bd...
ALL above should change to another url because matches /members/ string in the url
RewriteEngine on
RewriteCond %{THE_REQUEST} ^GET /members/(.*)
RewriteRule ^(.*)$ - [F,L]
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/members(.*)
RewriteRule ^(.*)$ - [F,L]
I have tried various combinations but does not seem to work. I'm sure I'm doing something really wrong as not on expert on this. Appreciate any pointers.
RewriteCond %{REQUEST_URI} ^/members
RewriteRule ^(.*)$ http://www.google.com/$1 [R, 301]
Would be something i would try to redirect a request for /members and send them to another domain. (with the same request uri). Further info on mod_rewrite can be found https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriterule
You are using the F flag in your rewrites. The flag is to send the Forbidden status back to client. If you want to redirect the user to another URL, you'll need to pass that URL as the second parameter to RewriteRule directive with the R flag:
RewriteEngine On
RewriteRule ^members\b http://some-other-website.com [R]

Rewrite part of query string with apache mod_rewrite

Need help with rewriting part of query string with mod_rewrite.
Looked through a lot of resources and have a general understanding of how stuff works but cannot figure out correct solution
This link:
http://example.com/?param=home&shop_id=1000005620&ate=bow&b_uid=-1&tg=one
Has to become this
http://example.com/?param=home/#/shop/1000005620?ate=bow&b_uid=-1&tg=one
If shorter, then this part of query string
&shop_id=1000005620& transforms into /#/shop/1000005620?
UPDATE:
Answer gave me a clear understanding what I had to do.
Exact rules that fixed my issue were like this:
<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} ^(.*)&shop_id=([0-9]{10,12})(?:&)(.*)$
RewriteRule ^(.*)$ https://%{SERVER_NAME}/$1?%1/#/shop/%2\?%3 [NE,L,R]
</IfModule>
<IfModule mod_rewrite.c>
RewriteCond %{QUERY_STRING} ^shop_id=([0-9]{10,12})$
RewriteRule ^(.*)$ https://%{SERVER_NAME}/#/shop/%1? [NE,L,R]
</IfModule>
Reason for this rewrite rules is hash handling with safari and ie
I have links that are used by external pages and also I have a redirection to https if site request http. If there is a hash in the link and request is sent from Safari or IE hash goes away from the URL and does not come back after redirection. I want to note very important fact that Chrome, Firefox do not have problems with keeping the URL with hash even after redirect to https. This involved to reconstruct our URL but it is worth it and now everything is working as it should.
You can try:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)&shop_id=([^&]+)&?(.*)$
RewriteRule ^(.*)$ /$1?%1/#shop_id=%2?%3 [L,R,NE]
EDIT:
what about if I only what to rewrite http://example.com/?shop_id=1000005620 into http://example.com/#/shop/1000005620 what the rewrite rule be then?
Just change the relevant parts of the regex pattern:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^shop_id=([^&]+)$
RewriteRule ^(.*)$ /$1/#shop_id=%2? [L,R,NE]

URL Rewriting using .htaccess

To change the URL /mobiles.php?id=5 to /mobiles/5
The content of .htaccess file is as follows:
Options +FollowSymlinks
RewriteEngine on
RewriteRule /mobiles/$1 ^/mobiles.php?id=([0-9]+)$
But still it is showing /mobiles.php?id=5 in the address bar. Please help. Is there anything else needs to be added in the .htaccess file?
Note:
mod_rewrite module is enabled
I have restarted Apache server after making changes to the .htaccess
file
.htaccess file is in htdocs folder of Apache.
I am using Windows + PHP + Apache + MySQL
This works for me:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^mobiles/([0-9]+)$ mobiles.php?id=$1&rew [L]
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^mobiles.php$ /mobiles/%1? [R,L]
If you see this line:
RewriteRule ^mobiles/([0-9]+)$ mobiles.php?id=$1&rew [L]
I have added rew variable in the query string to prevent Apache to fall in an infinite loop
When Apache execute this line:
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
Is to make sure that url has not been rewritten for Apache
If your only concern is that the old url stays in the address bar, and you want this not to happen, try adding an [R] at the end.
RewriteRule ^/mobiles.php?id=([0-9]+)$ /mobiles/$1 [R]
Did you actually see the correct page?
By the way, the rewrite rules generally go the other way. I would be expecting to see something like:
RewriteRule ^/mobiles/([0-9]+)$ /mobiles.php?id=$1
Is your concern one of making sure a URL with query parameters does not show up in the address bar?
If I understand correctly, you want
Internally redirect /mobiles/5 to /mobiles.php?id=5
Also redirect the browser TO /mobiles/5 if a user navigates to /mobiles.php?id=5
For this you need 2 rules one to internally rewrite the URL for 1st case and 2nd for browser redirection.
You can do it like this:
RewriteEngine on
# for internal rewrite
RewriteRule ^/?mobiles/([0-9]+)/?$ /mobiles.php?id=$1 [L]
# for browser redirect
RewriteRule ^/?mobiles\.php\?id=([0-9]+)$ /mobiles/$1/ [R,L]
You are doing the opposite, should be:
RewriteRule ^/something/mobiles/([0-9]+)$ /something/mobiles.php?id=$1

Redirect using .htaccess

I want to redirect all user page requests to a page on the same domain.
For example, I have an "under construction, BRB" page that I want all users to see when they try to access ANY page on the site.
I tried using this:
Redirect 302 / http://www.domain.com/index2.php
What that does is try to apply the redirect to the index2.php page as well and it gets stuck in a loop where the user then sees this until the browser stops.
http://www.domain.com/index2.phpindex2.phpindex2.phpindex2.php etc., etc,
Any idea on how to write that rule to except that page?
You have to exclude the file you want to redirect to. Here’s an example with mod_rewrite:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule !^index2\.php$ /index2.php [L,R=302]
</IfModule>
To prevent endless looping (index2.php <--> index2.php):
Only redirect If URL to be redirected does NOT contain the string 'index2.php'
RewriteCond %{QUERY_STRING} !index2.php
RewriteRule ^(.*)$ /index2.php [L,R=301]
You could use mod_rewrite
<IfModule mod_rewrite.c>
RewriteCond {REQUEST_URI} !=/index2.php
RewriteEngine on
RewriteRule ^.*$ /index2.php
# End .HTACCESS
</IfModule>
I'd be more inclined to use the slightly different
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/.*$ /index2.php [R=307,L]
</IfModule>
This will let you return a moved temporarily status for the redirect.
Omitting this set of flags means that mod_rewrite will return a 302 Found status by default.
HTH
cheers,