How to make this

Discuss practical ways rearrange URLs using mod_rewrite.

How to make this

Postby visi0n » Tue Jul 08, 2008 3:47 am

Hi,

How are the rules used in .htaccess to convert

this: www.example.com/playsong.php?id=1

in this: www.example.com/paysong/1/

Thanks.
visi0n
 
Posts: 5
Joined: Tue Jul 08, 2008 3:45 am

Friendly URLs with Mod_Rewrite

Postby enovar » Tue Jul 08, 2008 4:15 am

RewriteEngine On
RewriteRule ^paysong/([^/]*)$ /paysong.php?id=$1 [L]
enovar
 
Posts: 6
Joined: Thu Jul 03, 2008 9:19 am

Postby visi0n » Tue Jul 08, 2008 8:01 am

not working :(
visi0n
 
Posts: 5
Joined: Tue Jul 08, 2008 3:45 am

Postby enovar » Tue Jul 08, 2008 8:43 am

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^paysong/([^/]*)$ /paysong.php?id=$1 [L]

Make sure do you have mod_rewrite enable on the apache server
(httpd.conf)

the example is:
www.example.com/paysong/1
(with no slash at the end)

Note: Don´t expect that the url appears like this above, you must enter or create a link,
example:
<a href="www.example.com/paysong/1">paysong</a>
enovar
 
Posts: 6
Joined: Thu Jul 03, 2008 9:19 am

Postby richardk » Wed Jul 09, 2008 1:57 pm

To allow for a trailing slash, only match numbers and not be affected by MultiViews
Code: Select all
Options +FollowSymLinks -MultiViews

RewriteEngine On

RewriteRule ^paysong/([0-9]+)/$ /paysong.php?id=$1 [L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby zaidze » Thu Aug 07, 2008 4:41 am

I've been looking for a similar solution, and I happened to come across this thread, which I've found useful.

I'm simply running a test at the moment to see if it's feasible.

I'd like to convert a uri, such as http://www.somedomain.com/accounts/?user=178&x=9, to http://www.somedomain.com/accounts/178/?x=9

I have the following in my .htaccess file, which is in the root dir.

Code: Select all
Options +FollowSymlinks All -Indexes -MultiViews
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^accounts/([0-9]+)/\?page=([0-9]+)$ /accounts/?user=$1&page=$2 [QSA,L]
</IfModule>


Of course it doesn't work. Nothing happens.

I have tried a different solution...

Code: Select all
RewriteCond %{REQUEST_URI}%{QUERY_STRING} ^/accounts/?user=([0-9]+)&(.*)$ [NC]
RewriteRule ^accounts/.*$ /accounts/%1/?%2 [L]


but this returns the mesage 'The requested URL /accounts/76048/ was not found on this server.' The odd thing about that solution is that I couldn't even get as far as the 404 unless I used %{REQUEST_URI} and %{QUERY_STRING} in the RewriteCon, but if I echo $_SERVER['REQUEST_URI'] through PHP, it prints /accounts/?user=178&x=9, which is what you'd expect.

Your help would be appreciated.

I should noted that I've tried the Google test to see if mod_rewrite is actualy working, and it is.
zaidze
 
Posts: 2
Joined: Thu Aug 07, 2008 4:22 am

Postby richardk » Thu Aug 07, 2008 12:59 pm

REQUEST_URI in Apache does not include the query string but it does in PHP.

You don't need to match the other (page, x) parameters, the QSA (Query String Append) flag passes them on
Code: Select all
Options  All
Options +FollowSymLinks -Indexes -MultiViews

<IfModule mod_rewrite.c>
  RewriteEngine On

  RewriteRule ^accounts/([0-9]+)/?$ /accounts/?user=$1 [QSA,L]
</IfModule>
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby zaidze » Fri Aug 08, 2008 12:51 am

richardk wrote:REQUEST_URI in Apache does not include the query string but it does in PHP.

Ah ok. The reason I thought that Apache REQUEST_URI contained the query string was because I've read that it did on another site, but it obviously doesn't, like you say.

Anyway, I've run the amended RewriteRule that you kindly posted, and it initially didn't work, but I then remembered your previous post to OP about the Uri format in the anchor tag should be written as you intend it to be displayed, so that's what I did and... it works, so thanks very much for you help.
zaidze
 
Posts: 2
Joined: Thu Aug 07, 2008 4:22 am


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 83 guests

cron