Apache mod_rewrite: what am I doing wrong? - php

I've got a really simple rewrite in my .htaccess file, but it doesn't exactly work the way I want. This is my code:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^admin/?$ admin.php [L]
RewriteRule ^([0-9]*).*$ index.php?id=$1 [L]
</IfModule>
Basically what I want is that every page is like this: /0/title. The title is just to make the URL clearer for the user, but the number (id) should be passed to my PHP script. With this code the id is not passed to my index.php script. It is passed to that script if I just remove ".*" from the fourth row, but then URL's with text after the number don't get passed to my index.php file.
What am I doing wrong? How can I fix this?
Thanks!

You are trying to trap URL's like /0/title, but you don't have a slash / in your match pattern. Try this instead:
# Should match /01234/anything
# with the "/anything" optional
RewriteRule ^([0-9]+)(/.*)?$ index.php?id=$1 [L]

Related

How to change %20 to - using htaccess with this code and how to get that value in php

Hello i am trying to change %20 to - using htaccess file but i am not being able to do it. Please have a look below code.
# Turn Rewrite Engine On
RewriteEngine on
# Rewrite for index.php
RewriteRule ^home index.php [L]
#Rewrite for inner.php?u=####
RewriteRule ^user/([0-9a-zA-Z]+) inner.php?u=$1 [L]
#Rewrite for article.php?id=1&title=Title Goes Here (unlimited number of space can be here)
RewriteRule ^article/([0-9]+)/([0-9a-zA-Z_-]+) article.php?id=$1&title=$2 [L]
and i am trying to get the value in article.php and echo the value using php like below
<?php echo $_GET['title'] ?>
The url looks like below. Their is %20 in the url but i want to convert it in -
http://localhost/learning/article/1/i%20am%20title
And when i try to echo the title value it only display i other are not shown. Please help me to fix it. Thank you in advance. :)
Please try with below .htaccess code. ([^/]+) REGEX code will allow all the chars in the querystring. So it will allow spaces and your querystring value will not broke down up to first space now.
# Turn Rewrite Engine On
RewriteEngine on
# Rewrite for index.php
RewriteRule ^home index.php [L]
#Rewrite for inner.php?u=####
RewriteRule ^user/([^/]+) inner.php?u=$1 [L]
#Rewrite for article.php?id=1&title=Title Goes Here (unlimited number of space can be here)
RewriteRule ^article/([0-9]+)/([^/]+) article.php?id=$1&title=$2 [L]
Apache automatically rewrites %20 into space (\s) but your rule doesn't accept it. This should work:
RewriteRule ^article/(\d+)/([0-9a-zA-Z_-\s]+) article.php?id=$1&title=$2 [L]
In your PHP code you will may need use urldecode(). However, I highly recommend to ignore title in GET parameter and to use only ID (don't forget to check if it is the right type (eg. integer)). Reading the title from your database is more secure.

URL Rewrite for calendar site

I am trying to make the URL for my calendar site look nicer with .htaccess, but I can't get it to work.
I already have a rule, that removes the .php extension, and it works perfect. It looks like this:
RewriteEngine On
# turn on the mod_rewrite engine
RewriteCond %{REQUEST_FILENAME}.php -f
# IF the request filename with .php extension is a file which exists
RewriteCond %{REQUEST_URI} !/$
# AND the request is not for a directory
RewriteRule (.*) $1\.php [L]
# redirect to the php script with the requested filename
My current URL look like this:
http://mydomain.com/calendar/calendar-site?year=2013&month=november
... and I want to make it look like this:
http://mydomain.com/calendar/2013/November
The site works perfect without the rewrite, where I use $_GET[] to get the values for year and month in the URL, but with the rewrite on, it cannot get the values from the url.
I have tried these (not both at once of course):
RewriteRule ^calendar/([^/]*)$ /calendar-site.php?year=$1&month=$2 [L]
RewriteRule ^([^/]*)/([^/]*)$ /calendar-site.php?year=$1&month=$2 [L]
The first one creates a 404 page and the second one cannot get the values from the url + it mess up the stylesheet.
Hope you guys can help me out here :D
Thanks - Jesper
You were close, but in your first try you only included one matching group for the year and not one for the month. Include the character class twice to match the year which is captured to $1 and again for the month captured in $2. Use RewriteBase to set the root for your redirect. Note that the "month" value will be in the same case as in the URL.
RewriteEngine on
RewriteBase /
RewriteRule ^calendar/([^/]*)/([^/]*)$ /calendar/calendar-site.php?year=$1&month=$2 [L]
Test using http://htaccess.madewithlove.be/
input url
http://mydomain.com/calendar/2013/november
output url
http://mydomain.com/calendar/calendar-site.php
debugging info
1 RewriteBase /
2 RewriteRule ^calendar/([^/]*)/([^/]*)$ /calendar/calendar-site.php?year=$1&month=$2 [L]
This rule was met, the new url is http://mydomain.com/calendar/calendar-site.php
The tests are stopped because the L in your RewriteRule options

create SEO friendly URL using rewriterule

I try to rewrite my SEO URLs to some real GET requests, to handle in my PHP file.
I want to have these 2 cases to work:
mysite.com/company-profile -> index.php?action=company-profile
mysite.com/faq/howcanijoin -> index.php?action=faq&anchor=howcanijoin
I got the first case to work using the rule:
RewriteRule ^([A-Za-z0-9-]+)$ index.php?action=$1
For the second I tried also. I put this rule before the previous one:
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?action=$1&anchor=$2
But it's not working. Any suggestions? If I understand correctly inside each parenthesis goes variables $1, $2 etc?
Do this
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)/(.*)$ /index.php?action=$1&anchor=$2
This?
RewriteRule ^([A-Za-z0-9-]+)/([A-Za-z0-9-]+)$ index.php?action=$1&anchor=$2 [QSA,L]

Create SEO-friendly URL with PHP with just the Querystring Value

Is there a way use mod_rewrite to produce the result below?
Original URL:
http://www.domain.com/shop.php?id=newyork
to
SEO friendly URL
http://www.domain.com/newyork
I've seen plenty of example where the above URL can be converted to http://www.domain.com/shop/newyork but I actually don't want to display the word 'shop/' so just http://www.domain.com/newyork
I'd have a go with something like the following, off the top of my head
RewriteRule ^([a-zA-Z]*)$ www.example.com/shop.php?id=$1
Do bear in mind that anything after your root domain, will be piped into your shop.php script.
Yes, in your .htaccess file put
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/([^/.]+)$ shop.php?id=$1 [L]
</IfModule>
([^/.]+) will match anything that isn't a / or . and store that info,
$1 at the end outputs that info into your shop script.
[L] tells mod_rewrite to stop looking for rules if this one works.
Based on your example:
RewriteRule ^([a-z]+)$ /shop.php?id=$1 [L]
Would match newyork, alaska, hamburg but not highway-1.

URL Mod-Rewrite

I currently have a working URL:
http://example.com/security-services.php?service=fixed-camera-surveillance
and then I have PHP say, $_REQUEST['service'] to do some stuff...
But I'd like to achieve the same function if the URL looked like this:
http://example.com/security-services/fixed-camera-surveillance
Thanks!
An .htaccess file with something like this should do it.
Options +FollowSymLinks
RewriteEngine On
RewriteRule security-services/(.*)/? security-services.php?service=$1 [L]
The part that says security-services/(.*)/? matches the URL in the browser and rewrites it to security-services.php.
The key part is the (.*) which captures that portion of the URL and passes it to the PHP script as a GET value.
Try this rule:
RewriteEngine on
RewriteRule ^security-services/([^/]+)$ security-services.php?service=$1 [L]
The [^/]+ describes the next path segment after /security-services, (…) forms a group that’s match then can referenced to with $1 in the substitution.
And if you want a more general for any kind of …-service.php file:
RewriteEngine on
RewriteRule ^([^/-]+)-services/([^/]+)$ $1-services.php?service=$2 [L]
You can create a rule like this:
RewriteRule ^security-services/(.*)/? /security-services.php?service=$1
If you're using a .conf file, change ^ to ^/

Categories