1

I've just started to use .htaccess and i'm having an issue here.

Inside my public_html folder i have these files.

  • index.php
  • profile.php
  • test.php
  • .htaccess

And when i go to profile.php file i have some parameters.

http://website.com/profile.php?id=1&name=Doe

For showing better SEO links i'm trying to make it appear like this

http://website.com/1/Doe

with this rewrite condition

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ profile.php?url=$1 [QSA,L]
</IfModule>

depended on this answer (click here)

and then in my php file i get the id of the user so i can make the queries etc with this code

$path_components = explode('/', $_GET['url']);
$ctrl=$path_components[0];

But the thing is that if i do so every file in my folder is trying to make the Rewrite Rule but i want a specific file... the profile.php one.

EDiT 1:

So from the comment below i made this htaccess file, and regardless the mod_rewrite is enabled on my server, the link is not changing at all.

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI} ^/profile.php$ [NC]
    RewriteRule ^([^/.]+)/([^/.]+)(?:/)?$ /profile.php?id=$1&name=$2 [L]
</IfModule>

And my php file now..

$path_components = explode('/', $_GET['id']);
$ctrl=$path_components[0];

But the link

http://website.com/profile.php?id=1&name=Doe

is not changing at all.

Community
  • 1
  • 1
Konstantinos Natsios
  • 2,874
  • 9
  • 39
  • 74
  • 2
    You are missing a `RewriteCond`. Take a look at the documentation of apache's rewriting module: http://httpd.apache.org/docs/current/mod/mod_rewrite.html – arkascha Jun 24 '16 at 08:09
  • @arkascha i have edited my answer. Thanks for the info but still the link is not changing at all. – Konstantinos Natsios Jun 24 '16 at 08:29
  • What _link_ are you talking about? – arkascha Jun 24 '16 at 08:30
  • `http://website.com/profile.php?id=1&name=Doe` it stays the same regardless the .htaccess file conditions @arkascha – Konstantinos Natsios Jun 24 '16 at 08:31
  • 1
    A rewrite rule cannot change a link. It modifies an _incoming_ request, that is something that is sent after an existing link has been clicked for example. It cannot somehow magically change the world around it. If you create some link and write it somewhere, then that link is what it is. – arkascha Jun 24 '16 at 08:32
  • What url are you going to and what do you expect to happen? – Amit Verma Jun 24 '16 at 08:33
  • @starkeen when i click a link with `href="http://website.com/profile.php?id=1&name=Doe" ` to show the link in the browser `http://website.com/1/Doe/` – Konstantinos Natsios Jun 24 '16 at 08:36
  • That makes little sense. For SEO purposes you need to publish the link `http://website.com/1/Doe/`. Then, when that link is clicked, you _internally_ translate it back to the ugly form which can be processed internally. That is what rewriting is for. – arkascha Jun 24 '16 at 08:43
  • @arkascha and when i put the `http://website.com/1/Doe` link on my browser, with the above htaccess file, it shows me 404 error – Konstantinos Natsios Jun 24 '16 at 08:45
  • That means that your rewriting rule is not (yet) working. With the current `RewriteCond` it certainly will not match, since `%{REQUEST_URI}` of the _incoming_ request does not start with `/profile`, but with `/1`... – arkascha Jun 24 '16 at 08:47

2 Answers2

1

You need to redirect your old url to the new one, put this above your existing rule

RewriteEngine on
RewriteCond %{THE_REQUEST} /profile\.php\?id=([0-9]+)&name=(.+)\sHTTP [NC]
RewriteRule ^ /%1/%2? [L,R]
RewriteRule ^([^/]+)/([^/]+)/?$ /profile.php?id=$1&name=$2 [L]
Amit Verma
  • 40,709
  • 21
  • 93
  • 115
0

Hello the issue is simple: you should exclude existing files/folders from rewriting process. I would recommend to have only one index.php page where you get all the request and process in one place. Anyway your working htaccess file should look like this:

<IfModule mod_rewrite.c>

    RewriteEngine On

    # exclude real folders/files
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    #RewriteRule ^ index.php [L] - # redirect anything to index
    # for your version
    RewriteRule ^(.*)$ profile.php?url=$1 [QSA,L] 
</IfModule>
George G
  • 7,443
  • 12
  • 45
  • 59
  • too many errors? ur wrong. it was maybe only last line comment after piece of code. I've updated – George G Jun 24 '16 at 09:18
  • Op wants to redirect url with query strings to a sef format (from old to new) Your example just rewrits the new url to old. – Amit Verma Jun 24 '16 at 09:27