|Friendly URL help|
| 7:25 am on Apr 25, 2012 (gmt 0)|
I have the link below which I created to test the functionality of the script I wrote. But because the rest of my site is setup with friendly or extensionless URLs, I want to do the same with this one:
echo "<a href='meta-paginated.php?page=$i'>$i</a> ";
This is the current rewrite rule I have in my .htaccess which works fine for normal links:
RewriteRule ^(([^/]+/)*[^/.]+)$ index.php?page=$1 [L]
So the question is,because I cant work it out.
1. What would the regex be for this scenario?
2. How would I need to change the link to work with the new rewrite rule and still select ?page 1,2,3,4,5, etc etc. The page ID is always a number?
Many thanks in advance. If you are kind enough to provide the solution would you be able to explain how you get there so I can learn and pass on my knowledge to other forum users in the future?
| 7:59 am on Apr 25, 2012 (gmt 0)|
Just to add:
This is the link in a raw format:
This is what the URL looks like:
This is the rule I think it should be but not completely sure:
RewriteRule ^meta-paginated/(.*)/(.*)/$ /meta-paginated.php?$1=$2
I am also not sure of the LINK construct that is why I havent listed it here.
[edited by: eelixduppy at 1:20 pm (utc) on Apr 25, 2012]
[edit reason] unlinked [/edit]
| 8:52 am on Apr 25, 2012 (gmt 0)|
Use example.com in this forum.
Never use (.*) in the middle of a pattern. It means capture everything to the end of the input.
The link should specify the URL format that you want your users to see and use. You can make it whatever you want. The pattern in the rule then has to match that format in order for the rewrite to happen.
I would use
href="/mp-4323" or similar for the link. The rule pattern then has to test for "mp-" and for "digits", capture the digits and rewrite to the named .php file passing ?page=$1 to it.
| 9:24 am on Apr 25, 2012 (gmt 0)|
I found a site that gave me some explanation about friendly URLs, which seemed useful, although I understand there may be times when it is out of scope.
This is the example from the link above:
RewriteRule ^product-([0-9]+)-(.+)?$ product.php?id=$1
This is the rule I compiled:
RewriteRule ^mp-([0-9]+)-(.+)?$ meta-paginated.php?page=$1 [L]
This is how my link looks now in raw format:
so hopefully the URL will look like so:
http://www.example.com/mp-2 or what ever the value is.
Is this correct and will it work properly?
Just to add to that:
I tested it and this is the result. Obviously it is not working correctly:
Warning: include(admin/mp-2.php) [function.include]: failed to open stream: No such file or directory in /var/www/test/index.php on line 42
Warning: include() [function.include]: Failed opening 'admin/mp-2.php' for inclusion (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/test/index.php on line 42
[edited by: chrisguk at 9:57 am (utc) on Apr 25, 2012]
| 9:45 am on Apr 25, 2012 (gmt 0)|
The linked-to example code is a good start, but there are several major flaws.
The text after the page number is optional in the pattern. That's a duplicate content issue. The text isn't passed to the script to check it matches for this page number. That's another duplicate content issue.
/product-45433-acme-rotating-widget both return the same content.
/product-45433-this-is-overpriced-junk both return the same content.
Don't make the text optional. Pass the $2 parameter to the script. Reject request if $2 isn't correct for the requested $1.
The tutorial also omits the required [L] flag and allows URLs with spaces, but that's entirely another matter.
Your current RewriteRule code fails because
(.+)? is for capturing optional text but the
^mp-([0-9]+)- part says that a hyphen is required after the page number.
Your RewriteRule code is looking for requests like
/p-2-something but you are requesting just
If you just want page number and no text in the link, the
-(.+)? part can be discarded from your RewriteRule code.
RewriteBase / is the default and can be omitted.
Add a leading / before the target .php filename in the RewriteRule. Get into the habit of doing this for all internal rewrites.
In your PHP code,
should be something like
<a href='mp-<?php print $i; ?>'><?php print $i; ?></a>
<?php print '<a href=\'mp-' . $i . '\'>' . $i . '</a>';
The above assumes the URL requests are for root pages and that the file that delivers the content is in the root folder of the server.
Adjust the rule pattern and/or the target filepath if either of those are untrue.
If you have multiple rules, the "more specific" rules must be listed first.