Htaccess rewrite rule redirected but server not processh

Viewed 33

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?

1 Answers

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