htacces url-rewrite for multiple variables - .htaccess

I have the following url-rewriting in htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^category/([0-9]+)/([0-9a-zA-Z]+)/?([0-9a-zA-Zßäöü\s]+)?/?([0-9a-zA-Z.ßäöü\s]+)?/?([0-9a-zA-Z.ßäöü\s]+)?/?([0-9]+)? category.php?mid=$1&cid=$2&ccid=$3&cccid=$4&ccccid=$5&page=$6 [NC,L]
category/$1/$2 should open catagory.php?mid=$1&cid=$2 (and will start with page=1); this works fine with the above code.
However, I would like the following url's to work as well"
category/$1/$2/$6 should open category.php?mid=$1&cid=$2&page=$6.
Please note $6 is always a number, the categories $2, $3, $4 and $5 are a combination of numbers and letters.
Examples:
category/19/sport (mid=19, cid=sport)
category/19/sport/2 (mid=19, cid=sport, page=2)
category/19/sport/soccer/2.championsleague/2016results (mid=19, cid=sport, ccid=soccer, cccid=2.championsleague, ccccid=2016results)
category/19/sport/soccer/2.championsleague/2016results/10 (mid=19, cid=sport, ccid=soccer, cccid=2.championsleague, ccccid=2016results, page=10)
How should I alter the URL rewrite?

You may use these rule in your site root .htaccess:
Options -MultiViews
RewriteEngine On
# skip all rules below for real files and directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^category(\d+)/([^/]+)/?$ category.php?mid=$1&cid=$2 [L,NC,QSA]
RewriteRule ^category(\d+)/([^/]+)/(\d+)/?$ category.php?mid=$1&cid=$2&page=$3 [L,NC,QSA]
RewriteRule ^category(\d+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ category.php?mid=$1&cid=$2&ccid=$3&ccccid=$4&cccccid=$5 [L,NC,QSA]
RewriteRule ^category(\d+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/(\d+)/?$ category.php?mid=$1&cid=$2&ccid=$3&ccccid=$4&cccccid=$5&page=$6 [L,NC,QSA]

Related

htaccess redirecting to the wrong page when using multiple rewriterules

I have a htaccess file which looks like the following:
Options -Indexes
RewriteEngine On
# no php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# only allow rewriting to paths that dont exist
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# example.com/username
RewriteRule ^([\w\d_\-]+)/?$ profile.php?uid=$1 [L,QSA]
# example.com/username/$tab
RewriteRule ^([\w\d_\-]+)\/([\w]+)/?$ profile.php?uid=$1&tab=$2
# example.com/listen/$id
RewriteRule ^listen\/([\w\d_\-]+)?\/?([\w\d_\-]+)?$ track.php?id=$1&secret=$2 [L,QSA]
The way it is supposed to work is to redirect any URL looking like these:
1. example.com/example points to profile.php?id=example
or
2. example.com/example/tab points to profile.php?id=example&tab=tab
3. example.com/listen/example points to track.php?id=example
or
4. example.com/listen/example/code points to track.php?id=example&secret=code
The problem is that sometimes, a link which looks like the third one will point to the profile page. However, what's weirder, is that if example has a dash in it, it will point to the right place. This shouldn't be happening because my regex is matching after listen.
All help is appreciated.
Both of your listen/ rules should appear before other rules and moreover 2 RewriteCond are only being applied to next immediate RewriteRule.
You may use these rules to replace all of your code:
Options -Indexes
RewriteEngine On
# only allow rewriting to paths that don't exist
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# example.com/listen/$id
RewriteRule ^listen/([\w-]+)/?$ track.php?id=$1 [L,QSA,NC]
# example.com/listen/$id/secret
RewriteRule ^listen/([\w-]+)/([\w-]+)/?$ track.php?id=$1&secret=$2 [L,QSA,NC]
# no php extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# example.com/username
RewriteRule ^([\w-]+)/?$ profile.php?uid=$1 [L,QSA]
# example.com/username/$tab
RewriteRule ^([\w-]+)/([\w-]+)/?$ profile.php?uid=$1&tab=$2 [L,QSA]
Also note that \w means [a-zA-Z0-9_] so no need to add \d_ in character class.

Getting multiple variables with Mod Rewrite

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]

Rewrite index.php?lang=1 to index-en.html

my urls are index.php?lang=1, index.php?lang=2, etc for each home language
and i want to rewrite it to index-en.html, index-de.html, etc
I missed something, it doesn't work.
the second other rule is messing up, don't know.
here is my code :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^index.php?lang=1$ index-en.php [QSA, L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[^/\.]+(\.html)?$ index.php?url=$0 [QSA,L]
</IfModule>
Any idea ?
Thanks
You can't match against the query string in a RewriteRule, you need to match against the %{QUERY_STRING} variable in a condition. So instead of:
RewriteRule ^index.php?lang=1$ index-en.php [QSA, L]
You want:
RewriteCond %{QUERY_STRING} ^lang=1$
RewriteRule ^index\.php$ index-en.php? [L]
You also don't want that space inside the brackets. It'll confuse mod_rewrite and make it think the rewrite flags end with ,.

htaccess rewrite doesn't work

I'm new with htaccess and with a little help I could write this, but I'm having some problems and I don't know how to solve them
# General setup
RewriteBase /
RewriteEngine on
Options -Indexes
CheckSpelling off
# rewrite php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{THE_REQUEST} \ /+([^\?\ ]+)\.php
RewriteRule ^ /%1 [L,R=301]
# rewrite html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteCond %{THE_REQUEST} \ /+([^\?\ ]+)\.html
RewriteRule ^ /%1 [L,R=301]
# error pages
ErrorDocument 404 error
Problems:
I'd like to transform somefile?var=value to somefile/value and it doesn't work
If I write somefile/anything I get somefile without css style instead of being redirect to the error page
As I'm new with htacess I don't know if that file is secure enough
What I'd like:
Redirect any extension to a filename file (with no extension)
Protect my htaccess and directory and files
redirect any users?id=id_user to users/username
Do you know any way to construct the htaccess online? or Do you have any idea or advice to give me?
Thank you!!!!!!
You're not getting CSS probably because your links are relative URLs, and when you have multiple paths in your URL, the relative links all break. You can try adding this to your page headers:
<base href="/" />
The somefile/anything is because the -f together with `%{REQUEST_FILENAME} actually checks for path info requests, so you want to use this instead:
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f
and
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.html -f
For values, try adding this before your other rules:
RewriteCond %{THE_REQUEST} \ /+([^\?\ ]+)\.php\?var=([^&\ ]+)&?([^\ ]*)
RewriteRule ^ /%1/%2?%3 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.+)/([^/]+)$
RewriteCond %{DOCUMENT_ROOT}/%1.php -f
RewriteRule ^(.+)/([^/]+)$ /$1.php?var=$2 [L,QSA]
For users, you're not going to be able to use htaccess because the URL /users?id=userid does not contain the username. Mod_rewrite has no idea what user ID maps to what user name. You'll need to do this using a php script (like users.php).

Shorten url in expressionengine

I use the cms expressionegine. I want to shorten my url
from
http://sionvalais.com/live/sionvalais
to
http://sionvalais.com/sionvalais
'sionvalais' is the weblog name in EE
live is the subdirectory in which EE is installed. Would someone be so kind as to help me with this?
RewriteEngine on
RewriteBase /
# rewrites http://sionvalais.com/sionvalais/* to expression engine
# respectively http://sionvalais.com/live/sionvalais/index.php*
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond $1 !\/live*
RewriteRule ^sionvalais/?(.*)$ live/sionvalais/index.php?$1 [L]
# rewrites http://sionvalais.com/live/sionvalais* to http://sionvalais.com/sionvalais*
# and sets http status code 301, so search engines 'forget' about your live directory
RewriteCond $1 !\/index\.php*
RewriteRule ^live/sionvalais(.*)$ /sionvalais$1 [L,R=301]
Here's something to try in your .htaccess file:
RewriteEngine on
RewriteCond $1 !^live
RewriteRule ^sionvalais(.*)$ live/sionvalais$1
It may need more refinement, but it seems to work in a very basic sandbox.

Resources