I want a URL like this:
www.mywebsite.com/user
So I do:
RewriteRule ^([a-zA-Z0-9_/-]+)$ profile.php?user=$1
it is working.
but SOMETIMES I need a secound parameter, like:
www.mywebsite.com/user/22 = user?id=22
or I need a secound parameter like this:
www.mywebsite.com/user/images = user?pg=images
notice that I can have an user/id or user/pg for different actions.
Any ideas how can I do this?
You can have 3 rules like this:
Options -MultiViews
RewriteEngine On
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# to handle /user/22
RewriteRule ^user/(\d+)/?$ user.php?id=$1 [NC,L,QSA]
# to handle /user/images
RewriteRule ^user/(\w+)/?$ user.php?pg=$1 [NC,L,QSA]
# existing rule
RewriteRule ^([\w-]+)/?$ profile.php?user=$1 [L,QSA]
It's very easy:
RewriteRule ^user/([0-9]+)$ index.php?user=$1
RewriteRule ^user/([a-zA-Z0-9_/-]+)$ index.php?pg=$1
Related
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.
Hello I want to rewrite two values to static value on a link.
Sometimes link is like this: http://website.com/?s=page2
or like this: http://website.com/?m=page1&s=page2
I want to rewrite these links to these:
http://website.com/page2
http://website.com/page1/page2
You can use these 2 rules in your root .htaccess:
RewriteEngine On
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# single parameter rule
RewriteRule ^([^/]+)/?$ ?s=$1 [L,QSA]
# two parameters rule
RewriteRule ^([^/]+)/([^/]+)/?$ ?m=$1&s=$2 [L,QSA]
I have URL's like this:
http://www.example.com/en/product.php?id=23&t=page-title-here
I want change URL's to something like this :
http://www.example.com/en/product23/page-title-here/
Place the following in your /.htaccess file:
RewriteEngine on
# Step 1: Redirect file-based URIs to new 'pretty permalinks' and prevent looping
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteCond %{QUERY_STRING} ^id=(\d+)&t=([^/]+)$ [NC]
RewriteRule ^(en/product).php$ /$1%1/%2? [R=302,NE,L]
# Step 2: Rewrite above permalink to file-based URI
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(en/product)(\d+)/([^/]+)/?$ /$1.php?id=$2&t=$3 [L,QSA]
Update: If you want to match multiple languages (per your comment below), you can use a non-capture group that checks for exactly two characters instead of just en:
# Use this rule in step 1 above
RewriteRule ^((?:[a-z]{2})/product).php$ /$1%1/%2? [R=302,NE,L]
# Use this rule in step 2 above
RewriteRule ^((?:[a-z]{2})/product)(\d+)/([^/]+)/?$ /$1.php?id=$2&t=$3 [L,QSA]
Here is what I'm trying to accomplish:
(this .htaccess file is located in mysite.com/test/):
http://mysite.com/test/admin go to http://mysite.com/test/admin/index.php
http://mysite.com/test/contact go to http://mysite.com/test/contact.php
http://mysite.com/test/salt-lake-city/ go to http://mysite.com/test/index.php/city=salt-lake-city
http://mysite.com/test/salt-lake-city/deals/ go to http://mysite.com/test/deals.php?city=salt-lake-city
To start, I have:
RewriteRule ^(.*)/(.*)\.php$ $2.php?city=$1 [L]
(this handles the last 2). But, when I try to add the admin clause:
RewriteRule ^admin/ admin/index.php [L]
RewriteRule ^(.*)/(.*)\.php$ $2.php?city=$1 [L]
It messes up (the css is out of whack) etc.
Any thoughts?
Well .. if I had to do these rewrite rules, I would do them like this:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# do not do anything for already existing files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
# admin rewrite
RewriteRule ^admin$ admin/index.php [L]
# rewrite /contact --> /contact.php (and similar)
# add .php extension if such file does exist
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php [L]
# OR
# alternatively specify their names directly
# plus it is more precise for the example you have provided
# (that's if you need to rewrite only specific pages)
RewriteRule ^(contact|about|something)$ $1.php [L]
# /salt-lake-city/ --> /index.php?city=salt-lake-city
RewriteRule ^([^/]+)/$ index.php?city=$1 [QSA,L]
# /salt-lake-city/deals/ --> /deals.php?city=salt-lake-city
RewriteRule ^([^/]+)/deals/$ deals.php?city=$1 [QSA,L]
To start, I have:
RewriteRule ^(.*)/(.*)\.php$ $2.php?city=$1 [L]
(this handles the last 2).
Sorry, but I do not see how "it will handle the last 2". I see no .php in the last two URL examples you have provided.
It messes up (the css is out of whack) etc.
Well -- let's see how it will work with my rules. In any case -- it may also depends how you wrote links to css/images/js.
I am trying to write rules in .htaccess file.
I Wrote the rule like this:
RewriteCond %{QUERY_STRING} ^id=([0-9]+)&dgid=([0-9]+)$
RewriteRule destination_content-id-(.*)-dgid-(.*)\.htm$ destination_content.html?id=$1&dgid=$2 [L]
restarted the server.
Before it is having the following rule.
RewriteEngine on
# Parse out basename, but remember the fact.
RewriteRule ^(.*)\.html$ $1 [C,E=WasHTML:yes]
# Rewrite to document.phtml if exists...
RewriteCond %{REQUEST_FILENAME}.phtml -f
RewriteRule ^(.*)$ $1.phtml [S=1]
# ...else reverse the previous basename cutout.
RewriteCond %{ENV:WasHTML} ^yes$
RewriteRule ^(.*)$ $1.html
it works fine.
but my rule not working.
Could you please help me in solving the issue.
Thanks,
Srilu
Leave out the RewriteCond (It does not match the RewriteRule).
You'll just need the RewriteRule and I guess you want it to look like this:
RewriteEngine on
RewriteRule destination_content-id-([0-9]+)-dgid-([0-9]+)\.htm$ destination_content.html?id=$1&dgid=$2 [L]