0

I'm so newbie in apache, so it's a basic question but I couldn't solve this using another questions or links.
I tried to change my URL using .htaccess and I had two purposes;

  1. hide .php extension
  2. change some queryString from somefile.php?id=bar to somefile/id/bar which bar is a number or mixed string like T51-3.
    My queryString is http://localhost/payment/theme/ticket?id=770314 that I want change it to http://localhost/payment/theme/ticket/id/770314 or
    http://localhost/payment/theme/ticket/770314

I found this code:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]


## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]


in stackoverflow.com/this_question

This was working nice but didn't solve my second issue.
So, I searched for a while in this site and others and did find some sample codes like this one :

RewriteRule ^([a-zA-Z0-9_-]+)-([0-9]+)$ ticket.php?id=$2

or

RewriteRule ^(.*)$ /ticket.php?url=$1 [L]

and etc...

but none of these didn't work for me.
Some of them will make the server done and some don't have any affect at all. Would you please guide me to right way...
I looked at these questions:
htaccess rewrite for query string

how to change query string parameters with name using .htaccess?

How can I use .htaccess to hide .php URL extensions?

And these links:
http://simonecarletti.com/blog/2009/01/apache-query-string-redirects/
https://wiki.apache.org/httpd/RewriteQueryString

UPDATE :
This is my site strucure: site structure
There is an htaccess for important folders, like, forms, db, classes etc...
All my site pages are in the theme folder, as you can see in the picture.
In the root htaccess, there's just two lines that auto start sessions.

And I added your code to theme's htaccess.
Thanks in Advance

Community
  • 1
  • 1
Kiyarash
  • 2,437
  • 7
  • 32
  • 61

1 Answers1

0

You can use this code in

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /payment/theme/

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php\?(\w+)=([^\s&]+)\s [NC]
RewriteRule ^ %1/%2/%3? [R,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

# to handle http://localhost/payment/theme/ticket/id/770314
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\w+)/([\w-]+)/([\w-]+)/?$ $1.php?$2=$3 [L,QSA]

## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Thanks for reply, but unfortunatley didn't work again. :( – Kiyarash Jul 25 '15 at 08:47
  • :-) There's not error... This code change `*/somepage.php` to `*/somepage` and that's good for me, but when I have querystrings like `somepage.php?id=123456`, I expected to be `somepage/id/123456` but the result is `somepage?id=123456` – Kiyarash Jul 26 '15 at 13:35
  • Thanks again... well, the url is as I expect but there's 404 error after using this code when I have querystring. `*/somepage.php` is ok but `*/somepage.php?id=123` give me 404. – Kiyarash Jul 26 '15 at 14:20
  • yes...exactly. `http://domain.com/payment/theme/somepage.php?id=123456` will change to `http://domain.com/payment/theme/somepage/id/123456` but there's 404 error and server show: `OBJECT NOT FOUND` – Kiyarash Jul 26 '15 at 15:29
  • Now it's ok But it seems this can't find `css` and `js` files!!! because the page will show without any style... This show just html part... And thank you again... – Kiyarash Jul 26 '15 at 18:32
  • this situation is about queryStrings. The simple page names are OK. `*/somepage.php` will change to `*/somepage' but `somepage/id/123456` just show html part without any style...!!! – Kiyarash Jul 26 '15 at 18:35
  • Add this in the `` section of your page's HTML: `` to resolve css/js paths – anubhava Jul 27 '15 at 02:59