Getting multiple variables with Mod Rewrite - .htaccess

This my .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^products/([^/\.]+)/?$ /product.php?$1
RewriteRule ^category/([^/\.]+)/?$ /category.php?cat=$1&page=$2 [QSA,L]
the link i put into the browser is site.com/category/batteries/1/ where batteries is category and 1 would be the page. However, my site only gets the category (so site.com/category/batteries/ works, but site.com/category/batteries/1/ returns a 404.)
category.php gets the value being passed in cat, but not the value in page. I keep reading on this, but just can't understand where I'm going wrong.

You're missing a capturing group so $2 is never set. Try:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^products/(.*+)/?$ product.php?$1
RewriteRule ^category/(.*+)/(\d+)/?$ category.php?cat=$1&page=$2 [QSA,L]

Changing the regex worked. Just getting the text inbetween slashes instead of trying to find the digit gave me the desired result.
RewriteRule ^category/([^/]+)/([^/]+)/? /category.php?cat=$1&page=$2 [QSA,L]

Related

htaccess SEF URLS dont work with the word 'drills'

I have a strange problem where my SEF URLs just wont work for the word 'drills' I have just got around the issue by using a different word but Id like to know why this doesn't work.
This is my entire htaccess file:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/([0-9])/?$ /index.php?page=$1 [QSA,NC,L]
This works perfectly and redirects domain.com/page/1 to the correct domain.com/index.php?page=1
However, if I change the htaccess file to this:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^drills/([0-9])/?$ /index.php?drill=$1 [QSA,NC,L]
I just get a url not found error.
To find out where it was going wrong I edited the htaccess 1 letter at a time so page became drills and it all worked correctly until I changed the initial letter. So this works:
RewriteRule ^prills/([0-9])/?$ /index.php?drill=$1 [QSA,NC,L]
but this is a URL not found:
RewriteRule ^drills/([0-9])/?$ /index.php?drill=$1 [QSA,NC,L]
Is there something special about the word 'drills' that means I can't use it for SEF URLs?
To fix it I've just used training-drills instead but it's bugging me that I couldn't get it to work with just the word 'drills'

Optimize Long URLs with .htaccess

I have theses URLs
http://www.website.com/?goto=plaforms
http://www.website.com/?goto=offers&platform=dates
http://www.website.com/?goto=profile&member=1
http://www.website.com/?goto=product&offer=2
I want to change them using .htaccess to be like this:
http://www.website.com/plaforms
http://www.website.com/offers/dates
http://www.website.com/profile/1
http://www.website.com/product/2
I've tried this but not working
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?goto=$1
Honestly I have no clue how to do it.
UPDATE:
I have an index.php in which I call all pages
index.php
$page = $_GET['goto'];
$url = $page.'.php';
if(file_exists($url)):
include($url);
else:
header('location:'?goto=home');
endif;
The 1st param ?goto= calls for pages and the second param call data from database
You could write rewrite rules like these:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#The rules
RewriteRule ^([^\/]+)?$ /?goto=$1 [R=301,L]
RewriteRule ^offers\/dates$ /?goto=offers&platform=dates [R=301,L]
RewriteRule ^profile\/(\d+)$ /?goto=profile&member=$1 [R=301,L]
RewriteRule ^product\/(\d+)$ /?goto=product&offer=$1 [R=301,L]
The problem is some of the parameter values can not be derived from your fancy URLs. Therefore, you have to spell them out fully (like /offers/dates) or partially like /profile/x and /product/x). The only really generic rule is the first one that rewrites anything /xxx to goto=xxx. So, depending on y our URL space you may have to write a lot of rules.

Replace items with RewriteRule using .htaccess

I'm trying to use the Url rewrite using "RewriteRule".
I would use this link to show a new article in my blog:
https://www.example.com/article/how-to-visit-italy-in-winter
The original script (including vars) is
https://www.example.com/post.php?idpost=how-to-visit-italy-in-winter
This is my .htaccess
RewriteEngine on
RewriteRule ^article/$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .$ https://www.example.com/post.php?idpost=$1 [L]
but it doesn't work, the browser redirect to:
https://www.example.com/post.php?idpost=
I'm trying to resolve about 2 days witout success :(
Thank you.
You rule looks fine except for two things . Your $1 back reference is undefined and empty as you are not capturing any URI. To capture parts of the uri you need to use Regex capture-groups (.*) .
2) You need to use an absolute path instead of the full url as your RewriteRule`s destination if you want to silently redirect the url.
RewriteEngine on
RewriteRule ^article/$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule article/(.+) /post.php?idpost=$1 [L]

URL rewriting breaks when index.php?url=index (parameter is "index"). Why?

Weird issue: I'm using this .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
to get these URLs. Works perfectly.
http://www.example.com/cool/url
Problem
But when the ?url-parameter has the word index in it, like in
http://www.example.com/index/page
then $_GET["url"] is empty. I think my RewriteRule is broken (despite i've seen exactly this RewriteRule in lots of tutorials) and the removing of index.php from the URL also removes the index-parameter.
Question
How to fix, how to make URLs like index/page possible ?
This is due to MultiViews option.
Add this line on top of your .htaccess file to disable MultiViews:
Options -MultiViews

Twitter Like URL but with a #'s?

I am making a social networking website, and I can't get this done:
If the url is http://url.com/#username, I want it to show the http://url.com/user?u=username page, but show http://url.com/#username URL.
However, if there is no # in the beginning, treat it like a regular URL.
This is my .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^/(\d+)*$ ./user.php?u=$1
The last three lines are what I tried, however that does not work as I wanted it to. I thought it would work because RewriteRule ^/(\d+)*$ takes any URI after the slash, and rewrites it to .user.php?u=*, but that didn't work, so I am looking for some suggestions on what to do next.
From my understanding of your question, this should work.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^#(.+?)$ user.php?u=$1 [NC,L]
It requires you to have a # before the username and it passes it as a GET variable (u) However, a person can add other characters which could confuse it as a username.
/#username/muffins
But if you want this, and to have pages per username (ie, /#username/info, /#username/about, etc) you can just use the explode() function in PHP.
If you just want it to get the username and nothing else, you can try
RewriteRule ^#([A-Za-z0-9]+)$ user.php?u=$1 [NC,L]
Hope this helps! :)

Resources