10 Replies - 2918 Views - Last Post: 14 April 2014 - 05:51 AM

#1 CaptainDuude  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-April 14

URL Rewrite from PHP link to clean url with Htaccess

Posted 10 April 2014 - 04:05 PM

I want to rewrite the link http://example.com/<username> to http://example.com/u...p?u=<username>. This is seen in almost all websites. For example, http://www.facebook....com/<username>. I have searched this a lot and tried a lot of code, but none of them seemed to work. This is in my .htaccess file right now.
ErrorDocument 404 /404.php
Options -Indexes
Options +MultiViews
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{HTTP_HOST} ^clinkstr.com$ [NC]
RewriteRule ^(.*)$ http://www.clinkstr.com/$1 [L,R=301]
RedirectMatch 301 ^(.+)/$ $1

This post has been edited by Dormilich: 11 April 2014 - 01:13 AM


Is This A Good Question/Topic? 0
  • +

Replies To: URL Rewrite from PHP link to clean url with Htaccess

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: URL Rewrite from PHP link to clean url with Htaccess

Posted 10 April 2014 - 05:02 PM

Moved to the Web Servers & Hosting forum. This doesn't really have anything to do with PHP coding.


I see nothing in that HTACCESS file that seems to actually be attempting to rewrite the URL in the way you describe. What have you tried in order to get that to work?
Was This Post Helpful? 0
  • +
  • -

#3 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: URL Rewrite from PHP link to clean url with Htaccess

Posted 10 April 2014 - 05:25 PM

Usually the other way around is referred to clean url.

?p= is an argument in the _GET array.
/username/ is clean & seo friendly.
Was This Post Helpful? 0
  • +
  • -

#4 CaptainDuude  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-April 14

Re: URL Rewrite from PHP link to clean url with Htaccess

Posted 10 April 2014 - 06:00 PM

Something like
RewriteRule ^user\/(.+)$ user.php?u=$1
would world perfectly, but I don't want the /user there. I just want www.domain.com/<username> not www.domain.com/user/<domain>. When I try removing it, it simply just doesn't work. Sometimes I would get redirect loops, sometimes it would seem like I didn't even change the code. And I don't want it to redirect, I still want the link to show www.domain.com/<username> without it redirecting and showing the user.php?u=<username>.

I meant www.domain.com/user/<username>

I've also tried things like
RewriteRule ^([a-zA-Z0-9]+)/$ user.php?u=$1
. But none of them worked.
Was This Post Helpful? 0
  • +
  • -

#5 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: URL Rewrite from PHP link to clean url with Htaccess

Posted 10 April 2014 - 06:15 PM

RewriteRule ^(.*)$ user.php?u=$1


or

RewriteCond %{HTTP_HOST} user.php?u=$1
RewriteRule ^(.*)$ http://www.clinkstr.com/$1 [L,NC,QSA]


Was This Post Helpful? 0
  • +
  • -

#6 CaptainDuude  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-April 14

Re: URL Rewrite from PHP link to clean url with Htaccess

Posted 10 April 2014 - 07:04 PM

View Postno2pencil, on 10 April 2014 - 06:15 PM, said:

RewriteRule ^(.*)$ user.php?u=$1


or

RewriteCond %{HTTP_HOST} user.php?u=$1
RewriteRule ^(.*)$ http://www.clinkstr.com/$1 [L,NC,QSA]


The first one makes a redirect loop and the second one just doesn't work. the reason for the redirect loop is probably because it rewrites to http://www.clinkstr.....php?u=user.php and I have no idea why. And that page redirects you back to http://www.clinkstr.com
Was This Post Helpful? 0
  • +
  • -

#7 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: URL Rewrite from PHP link to clean url with Htaccess

Posted 11 April 2014 - 03:10 AM

Can you post the full .htaccess file?
Was This Post Helpful? 0
  • +
  • -

#8 CaptainDuude  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-April 14

Re: URL Rewrite from PHP link to clean url with Htaccess

Posted 11 April 2014 - 03:43 AM

View Postno2pencil, on 11 April 2014 - 03:10 AM, said:

Can you post the full .htaccess file?

# Begin cache control #
ExpiresActive on
ExpiresActive off
<FilesMatch ".*">
Header unset Cache-Control
Header unset Expires
Header unset Last-Modified
FileETag None
Header unset Pragma
</FilesMatch>
# End cache control #
DirectoryIndex index.php


ErrorDocument 404 /404.php


Options -Indexes +MultiViews


RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{HTTP_HOST} ^clinkstr.com$ [NC]
RewriteRule ^(.*)$ http://www.clinkstr.com/$1 [L,R=301]
RedirectMatch 301 ^(.+)/$ $1
RewriteCond %{HTTP_HOST} user.php?u=$1
RewriteRule ^(.*)$ http://www.clinkstr.com/$1 [L,NC,QSA]

Was This Post Helpful? 0
  • +
  • -

#9 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: URL Rewrite from PHP link to clean url with Htaccess

Posted 11 April 2014 - 04:06 AM

The following works for me :

RewriteEngine On
RewriteRule ^(.*)/$ user.php?u=$1 [L,QSA]



http://prayerscope.com/anything/

<?php
if(isset($_GET['u'])) {
  $user=$_GET['u'];
} else {
  $user="unset";
}
echo '<h1>User : '.$user.'</h1>';
?>



** Edit **
So I may have mis-read this.

Do you want /user/<user>
or did you want /<user>/ ?
Was This Post Helpful? 0
  • +
  • -

#10 CaptainDuude  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 10-April 14

Re: URL Rewrite from PHP link to clean url with Htaccess

Posted 11 April 2014 - 11:43 AM

View Postno2pencil, on 11 April 2014 - 04:06 AM, said:

The following works for me :

RewriteEngine On
RewriteRule ^(.*)/$ user.php?u=$1 [L,QSA]



http://prayerscope.com/anything/

<?php
if(isset($_GET['u'])) {
  $user=$_GET['u'];
} else {
  $user="unset";
}
echo '<h1>User : '.$user.'</h1>';
?>



** Edit **
So I may have mis-read this.

Do you want /user/<user>
or did you want /<user>/ ?

I wanted /<user>
Was This Post Helpful? 0
  • +
  • -

#11 no2pencil  Icon User is offline

  • Admiral Fancy Pants
  • member icon

Reputation: 5363
  • View blog
  • Posts: 27,325
  • Joined: 10-May 07

Re: URL Rewrite from PHP link to clean url with Htaccess

Posted 14 April 2014 - 05:51 AM

View PostCaptainDuude, on 11 April 2014 - 02:43 PM, said:

I wanted /<user>

That is what my provided example does.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1