1

I have this URL:

https://www.example.it/page.php?user=Pippo 

The result that I should have is:

https://www.example.it/Pippo 

I try with this code:

RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^page/(.*)/$ /page.php?user=$1 [L] 

But the part of the URL stays and if I go back to another page, I keep being redirected to the same page as long as the server crashes.

1
  • You've just changed the example code. But your first version looked correct, according to your example URLs? Also, do you have any other directives in your .htaccess file? The order is important. If your URLs really are of the form /page/<username>/ (as your updated code suggests) then you will need to make sure that MultiViews is also disabled. Commented Aug 30, 2018 at 22:43

1 Answer 1

1

If you want to redirect /page.php?user=<username> to /<username>, where <username> is a series of a-z or A-Z chars then try something like the following at the top of your .htaccess file:

Options -MultiViews RewriteEngine On RewriteCond %{ENV:REDIRECT_STATUS} ^$ RewriteCond %{QUERY_STRING} ^user=([a-zA-Z]+) RewriteRule ^page\.php$ /%1? [R=302,L] 

Just to clarify, this is only to satisfy search engines that might have already indexed the old URL, or where external sites have linked to it. The URLs in your application already link directly to /<username>.

The %1 is a backreference to the captured group in the last matched CondPattern. The check against the REDIRECT_STATUS environment variable is to ensure we are only checking the initial request and not a rewritten request.

Change to a 301 only once you have tested that it's working OK.

You can then follow this with your existing rule that internally rewrites the request back to /page.php?user=<username>. Although the NC flag on your directive is unnecessary. As is the check for an existing file (and probably the directory?), unless you have files without extension? And you don't need to escape the dot in a character class. So, it can be simplified to:

RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^([^.]+)$ /page.php?user=$1 [L] 
Sign up to request clarification or add additional context in comments.

4 Comments

I've updated my answer to disable MultiViews (in view of your updated code sample).
If I use the first solution, takes only the first two letters of the user. With the second, continue to redirect.
I've only really presented one solution. The two code snippets are to be used together. What format does the username take? The above assumes letters-only, as in your example. Only if the username contained a non-alpha chars would it take less than the full username. The rule block is not a "redirect", so if you are seeing a redirect then you may be seeing a cached response or there is a conflict with existing directives perhaps?
I had failed to change the regex! Now it works correctly! Thanks you very much!!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.