0

I have a .htaccess file where when a user signs up on my site, they'll be redirected to example.com/profiles/[username] . The ugly URL is example.com/profiles/?username=john where john can be any name. Whenever, the user searches for let's say example.com/profiles/[username] a 404 gets displayed, so instead I want this friendly URL to redirect to an ugly one so their profile can be displayed. So I want example.com/profiles/john to be redirected to example.com/profiles/?username=john internally.

Here's my .htaccess conditions:

RewriteCond %{THE_REQUEST} /profiles/\?username=([^&\s]+) [NC]
RewriteRule ^ /profiles/%1? [L,R=302]

RewriteRule ^profiles/([\w-]+)/?$ /profiles/?username=$1 [L]

These conditions successfully remove the ?username=john part of the URL, how do I make it so it's the other way around? So when someone searches for example.com/profiles/john it gets redirected to example.com/profiles/?username=john internally so the page gets successfully displayed?

Bytes
  • 691
  • 1
  • 7
  • 22
  • you say "internally". Do you want the browser to still think it's at `example.com/profiles/john` but for some script in the `profiles` directory to receive `username=john` as a `$_GET` key=>value? –  Jun 29 '16 at 04:13
  • @Terminus With the above rewrite rules, when I go to example.com/profiles/?username=john, it is redirecting the URL to example.com/profiles/john and displaying a 404 error like the page doesn't exist. So my solution to this was if the user types in the friendly URL, they'll be redirected to the ugly URL so the profile could be displayed and the 404 error would be gone. Preferably, I'd love it for the URL in the browser to stay in its friendly state and not change to the ugly one. But the friendly URL is displaying a 404. If that makes sense – Bytes Jun 29 '16 at 04:22
  • one other question then: do you have a `profiles` directory in your webroot and an `index.php` file in that directory (technically 2 questions but hey) –  Jun 29 '16 at 04:27
  • imo, just switching your rule to something like the one described here: http://stackoverflow.com/questions/8595964/redirect-all-traffic-to-index-php-using-mod-rewrite would benefit you. You can then use the index page to do the stuff you have to do on every page (connect to db, bring in your commonly used functions, etc...) You could then handle the "routing" by including files based on the `url` parameter (which i suggest you change to something not so common) –  Jun 29 '16 at 04:30
  • @Terminus With this added, i'm just getting a 500 error – Bytes Jun 29 '16 at 04:45
  • Sorry, what was added? –  Jun 29 '16 at 04:49
  • @Terminus I added RewriteRule ^(.*)$ /index.php?url=$1 [L,QSA] – Bytes Jun 29 '16 at 04:50
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/115916/discussion-between-terminus-and-bytes). –  Jun 29 '16 at 04:56

1 Answers1

0

To redirect /profiles/?username=user to /profiles.user you can use

RewriteCond %{THE_REQUEST} /profiles/\?username=([^&\s]+) [NC]
RewriteRule ^ /profiles.%1? [L,R=302]

RewriteRule ^profiles\.([^/]+)/?$ /profiles/?username=$1 [L]
Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • Sorry I meant I want to redirect example.com/profiles/user to example.com/profiles/?username=user – Bytes Jun 29 '16 at 03:57
  • @Bytes your orignal rule already rewrites it. what's the problem? – Amit Verma Jun 29 '16 at 04:07
  • My original rule rewrites example.com/profiles/?username=user to example.com/profiles/user but I don't want this. I want it to rewrite example.com/profiles/user to this example.com/profiles/?username=user – Bytes Jun 29 '16 at 04:09
  • Remove the external redirect (first 2 lines) leave this `RewriteRule ^profiles/([\w-]+)/?$ /profiles/?username=$1 [L]` – Amit Verma Jun 29 '16 at 04:20
  • With this nothing is happening. example.com/profiles/john isn't redirecting to example.com/profiles/?username=john it's staying the same and displaying a 404 error – Bytes Jun 29 '16 at 04:25