0

How this url:

www.domain.com/services.php?hello-world-be-active

can be rewritten to:

www.domain.com/services/hello-world-be-active

UPDATED:

Below rule is not working... I am using this rule

RewriteCond %{THE_REQUEST} ^GET\ /(.+)\.php [NC]
RewriteRule ^ /%1 [QSA,R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /$1.php

Above Rule gives the following Results:

www.domain.com/whatever.php => www.domain.com/whatever (Correct)
www.domain.com/whatever.php?whatever-whatever => www.domain.com/whatever?whatever-whatever (Not Correct because question mark '?' should replace with slash '/')
ma rafa
  • 55
  • 1
  • 2
  • 10

1 Answers1

0

Try this:

Options +FollowSymLinks

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /

    # Match the host, if you want
    RewriteCond %{HTTP_HOST} ^www\.domain\.com$

    RewriteCond %{REQUEST_URL} !\.php$
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^[^/]*/(.*)$ %{REQUEST_FILENAME}.php?$1 [QSA,L]
</IfModule>
## Results:
# services/whatever-here         => services.php?whatever-here
# services/whatever-here?foo=bar => services.php?whatever-here&foo=bar
# anything/whatever              => anything.php?whatever
# services/foo/bar               => services.php?foo/bar

With these rules in place, any call to services/whatever will be mapped to the services.php file. In case you add an R flag to the end of the rewrite rule, the request will be externally redirected to services.php instead.

But if you want it the other way around; meaning to redirect services.php?whatever to services/whatever, here's what you need:

Options +FollowSymLinks

<IfModule mod_rewrite.c>
    RewriteEngine on
    RewriteBase /

    # Match the host, if you want
    RewriteCond %{HTTP_HOST} ^www\.domain\.com$

    RewriteRule (.+)\.php $1/%{QUERY_STRING} [R,QSD,L]
</IfModule>
## Results:
# foo/bar/baz.php?qux   => foo/bar/baz/qux
# services.php?whatever => services/whatever
sepehr
  • 17,110
  • 7
  • 81
  • 119
  • Thank you for your answer. But If there is something else instead of services like http://www.domain.com/whatever.php?whatever to http://www.domain.com/whatever/whatever as this url is dynamic for all web pages so there should be a common solution for all those pages also. – ma rafa Jan 09 '17 at 14:53
  • Thank you for quick answer. But this is not working for me... I have used both options but not able to get the desired results.. I have updated my question above. Kindly check! – ma rafa Jan 11 '17 at 15:25