mod_rewrite conflict hopefully simple fix?

Discuss practical ways rearrange URLs using mod_rewrite.

mod_rewrite conflict hopefully simple fix?

Postby jason27 » Fri Jul 25, 2008 1:36 pm

Hello, I am new and wanted to say thanks for providing us with this forum. I am using mod rewrite on a page so that a user can enter a url such as:

blah.com/1
(integer)

or:

blah.com/1/
(trailing slash)

or:

blah.com/username
(text)

It works by using this mod_rewrite code:

Code: Select all
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d

# User public music pages

RewriteRule ^(.*)(\/)?$ music.php?v=$1 [L]


The problem is that if I want to do a simple rewrite of a link inside of those pages to make the link shorter/SEO purposes, there is a conflict. For example if I rewrite a link within that page such as and add this to the htaccess file:

Code: Select all
RewriteRule a-(.*)-b-(.*)\.html$ extra.php?a=$1&b=$2


If I call it up in the browser it doesn't direct to that page but to the value from the first rewrite code. Could you please provide me with working code that allows the rewrite to still work but allows me to rewrite other links as well? Thanks :)
jason27
 
Posts: 7
Joined: Fri Jul 25, 2008 1:32 pm

Postby richardk » Fri Jul 25, 2008 3:39 pm

^(.*)(\/)?$ matches everything. The conditions make it ignore existing files and directories. This is why it matches your other URL.

If you do the other rules first, it should work
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^a-([^/]+)-b-([^/]+)\.html$ /extra.php?a=$1&b=$2 [QSA,L]

# User public music pages
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^([^/]+)/?$ music.php?v=$1 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby jason27 » Fri Jul 25, 2008 3:44 pm

Thanks for the quick reply I have read your other replies eariler and you really know your stuff :) I will try it now thanks again.
jason27
 
Posts: 7
Joined: Fri Jul 25, 2008 1:32 pm

Postby jason27 » Fri Jul 25, 2008 3:53 pm

Thanks a lot it works :D I could never get a grasp of mod_rewrite or pregmatch type coding since I sometimes forget the values of all of the symbols. I am mainly a php guy :P Thanks again!
jason27
 
Posts: 7
Joined: Fri Jul 25, 2008 1:32 pm

Postby jason27 » Sat Jul 26, 2008 5:06 am

Sorry one last minor thing please. How can I rewrite the urls so I can add each page manually to htaccess and only have the ID field a variable? For example, I have this:

Code: Select all
RewriteRule ^(.*)-(.*)\.html$ /test/extra.php?a=$1&b=$2 [QSA,L]


Which as you know a = anything and b = anything. The problem with that is if I rewrite another file other than extra.php on the same page such as user_watch.php:

Code: Select all
RewriteRule ^watch-(.*)\.html$ /test/user_watch.php?b=$1 [QSA,L]


It conflicts between watch- and (.*)-. As a quick fix I tried to give each entry different slashes and underscores:

Code: Select all
RewriteRule ^(.*)-(.*)\.html$ /test/extra.php?a=$1&b=$2 [QSA,L]
   
RewriteRule ^watch(.*)\.html$ /test/user_watch.php?b=$1 [QSA,L]
   
RewriteRule ^(.*)_(.*)\.html$ /test/extra.php?a=$1&song_id=$2 [QSA,L]


If you look closely you can see how sometimes the second value is b= or song_id=. It works but is there a way where I can just enter every link individually to avoid conflicts and they all use the same url style? I use a script on another site that managed to pull this off but I couldn't get it to work with this script. Can you please provide me with a working example? Thanks for your time :)
jason27
 
Posts: 7
Joined: Fri Jul 25, 2008 1:32 pm

Postby richardk » Sat Jul 26, 2008 2:48 pm

If you do the watch- rule first, it will work.
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^watch(.*)\.html$ /test/user_watch.php?b=$1 [QSA,L]
RewriteRule ^(.*)-(.*)\.html$ /test/extra.php?a=$1&b=$2 [QSA,L]


But you cannot have
Code: Select all
RewriteRule ^(.*)-(.*)\.html$ /test/extra.php?a=$1&b=$2 [QSA,L]
RewriteRule ^(.*)-(.*)\.html$ /test/extra.php?a=$1&song_id=$2 [QSA,L]


I suggest you don't use .* because it matches everything and causes some of these problems. You should try and craete an appropriate, but very limited, character class. Then if song_id is always a number, and b never is only a number, your could have
Code: Select all
RewriteRule ^(.*)-([0-9]+)\.html$ /test/extra.php?a=$1&song_id=$2 [QSA,L]
RewriteRule ^(.*)-(.*)\.html$ /test/extra.php?a=$1&b=$2 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby jason27 » Sat Jul 26, 2008 5:54 pm

Thanks again for the reply. In your first example:

Code: Select all
RewriteRule ^watch(.*)\.html$ /test/user_watch.php?b=$1 [QSA,L]
RewriteRule ^(.*)-(.*)\.html$ /test/extra.php?a=$1&b=$2 [QSA,L]


I am aware of the problem just trying to find the solution :P Right now I am removing the dash from the watch url to avoid conflicts between the extra.php file and other files. I am asking if I can make all of the urls appear the same "style" with different files and values. Such as:

fav-(number).html
comments-(number).html
watch-(number).html
play-(number).html

Instead of having:

watch(number).html
fav-(number).html
play_(number).html

Yes b and song_id are always numeric so I made the change you have suggested by using ([0-9]+). Here is how my htaccess file looks now I basically want to be able to keep every url looking the same with the url name then the value:

Code: Select all
Options +FollowSymLinks

RewriteEngine On


# Rewrite links in music pages. NOTE: On the real site remove /test from the url.


   RewriteRule ^(.*)-([0-9]+)\.html$ /test/extra.php?a=$1&b=$2 [QSA,L]
   
   RewriteRule ^watch([0-9]+)\.html$ /test/user_watch.php?b=$1 [QSA,L]
   
   RewriteRule ^(.*)_([0-9]+)\.html$ /test/extra.php?a=$1&song_id=$2 [QSA,L]


# Public musician url name

   RewriteCond %{SCRIPT_FILENAME} !-f
   RewriteCond %{SCRIPT_FILENAME} !-d
   RewriteRule ^([^/]+)/?$ music.php?v=$1 [QSA,L]
jason27
 
Posts: 7
Joined: Fri Jul 25, 2008 1:32 pm

Postby jason27 » Mon Jul 28, 2008 10:20 pm

If you do not understand what I mean I can pm you the site url. Basically I would like all of the urls to have the same style (action) (dash) (value).html instead of having different url styles:

(action) (dash) (value).html
(action) (underscore) (value).html
(action) (no space) (value).html

On the same page. Thanks :)
jason27
 
Posts: 7
Joined: Fri Jul 25, 2008 1:32 pm

Postby richardk » Tue Jul 29, 2008 11:40 am

The watch rule in my last post was supposed to include a dash.

You should be able to do this
Code: Select all
Options +FollowSymLinks

RewriteEngine On

# watch- URLs.
RewriteRule ^watch-([0-9]+)\.html$ /test/user_watch.php?b=$1 [QSA,L]

# play- URLs.
RewriteRule ^play-([0-9]+)\.html$ /test/extra.php?a=play&song_id=$1 [QSA,L]

# Everything else.
RewriteRule ^([^/]+)-([0-9]+)\.html$ /test/extra.php?a=$1&b=$2 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby jason27 » Wed Jul 30, 2008 1:01 am

That didn't work but you did get me going in the right direction so I I got it working now. Basically I wanted to assign each url manually such as:

Code: Select all
RewriteRule ^stations-([0-9]+)\.html$ /test/extra.php?a=stations&b=$1 [QSA,L]

RewriteRule ^fans-([0-9]+)\.html$ /test/extra.php?a=fans&b=$1 [QSA,L]

RewriteRule ^watch-([0-9]+)\.html$ /test/user_watch.php?b=$1 [QSA,L]


to avoid conflicts and works perfectly. Thanks again for your great help :)
jason27
 
Posts: 7
Joined: Fri Jul 25, 2008 1:32 pm


Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: No registered users and 35 guests

cron