My website page what I am recently working on is mydomain.com/page.php?cat=cover&brand=Nokia.
I want to display the url something like this -
mydomain.com/cover/nokia
Meaning I don't want to show the page name i.e. page.php and hide the parameter names as well. Also the last parameter is optional.
If somebody can help me doing this, will be a great help.
Thanks
You can use these rules in root .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/?$ page.php?cat=$1&brand=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ page.php?cat=$1 [L,QSA]
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.
Here is part of my .htaccess file.
RewriteEngine On
RewriteBase /example/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule tilaukset home.php?page=orders
RewriteRule tilaustenhallinta home.php?page=ordersManage
RewriteRule jarjestelmaraportit home.php?page=ordersReports
RewriteRule kaikki-tilaukset home.php?page=allOrders
RewriteRule keskeneraiset-tilaukset home.php?page=inProgressorders
RewriteRule valmiit-tilaukset home.php?page=completedOrders
RewriteRule tuotannossa-tilaukset home.php?page=inProductionorders
RewriteRule lahetetyt-tilaukset home.php?page=shippedOrders
By clicking this element Tilaukset it redirects to correct place (which is orders.php), but when I click another link with different properties Kaikki tilaukset it still opens the same file. When I remove "tilaukset" from "Kaikki tilaukset" and just leave it as "Kaikki". Like so:
RewriteRule kaikki home.php?page=allOrders
It works correctly and nothing bad happens. For some reason it doenst check the whole argument. It finds same word and then uses that. How to fix this?
This line :
RewriteRule tilaukset home.php?page=orders
means any URI contains tilaukset it will capture a kaikki-tilaukset as well
change it to :
RewriteRule ^tilaukset home.php?page=orders
This ^ means start with tilaukset .
The other Rule:
RewriteRule kaikki-tilaukset home.php?page=allOrders
Should work fine right now.
Is there a way to get this to work?
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ index.php?a=1&b=2 [L]
RewriteRule ^([^/]+)/([^/]+)$ index.php?a=$1&c=$2 [L]
The urls should look like this:
http://test.com/a/b
or
http://test.com/a/c
It only works with the first rule.
In my case I'm trying to create a profile page and get the ID either of the session ID or via $_GET['id'].
So if I visit a profile of someone else the url is
index.php?page=profile&id=25
/profile/25
And if I'm visiting my own profile it is
index.php?page=profile
/profile
And for example I want to edit my profile it is
index.php?page=profile&action=edit
/profile/edit
I hope you understand what I mean and can help me.
The key to solving this is noticing the differences between each parameter you want to pass.
If the visitor is looking at someone else's profile, an id (numeric) is passed.
If the visitor is editing their profile, a parameter string (alphanumeric) is passed
If the visitor is looking at their own profile, or another generic page, no extra parameters are passed
.htaccess rules can be most easily written from the most specific to the most general, so translating this, the rules become
RewriteRule ^([^/]+)/([0-9]+)$ index.php?page=$1&id=$2 [L]
RewriteRule ^([^/]+)/([a-z0-9]+)$ index.php?page=$1&action=$2 [NC,L]
RewriteRule ^([^/]+)$ index.php?page=$1 [L]
It is also important to skip existing files & directories, however because RewriteCond statements only match the next RewriteRule, it is easiest to do this in a slightly different way than you are doing it.
RewriteEngine On
# If the file or directory exists, exit
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .? - [END]
RewriteRule ^([^/]+)/([0-9]+)$ index.php?page=$1&id=$2 [L]
RewriteRule ^([^/]+)/([a-z0-9]+)$ index.php?page=$1&action=$2 [NC,L]
RewriteRule ^([^/]+)$ index.php?page=$1 [L]
I have tried to create an .htaccess file to do following:
Direct www.domain.com/name or www.domain.com/name/ to www.domain.com/page.php?id=name
and www.domain.com/name/2 or www.domain.com/name/2/ to www.domain.com/page.php?id=name&pg=2
my .htaccess looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# If the request is not for a valid directory
RewriteCond %{REQUEST_FILENAME} !-d
# If the request is not for a valid file
RewriteCond %{REQUEST_FILENAME} !-f
# If the request is not for a valid link
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^([a-z0-9-_\.]+)/?$ page.php?id=$1 [L]
RewriteRule ^([a-z0-9-_\.]+)/([a-z0-9]+)/?$ page.php?id=$1&pg=$2 [L]
</IfModule>
The problem is, that when I actually use a slash after name it thinks of it as a directory and looks for pages in www.domain.com/name/.. But I am still able to $_GET the variables based on id and pg.
Can anyone tell me what I have done wrong? I prefer that the URL in the address bar stays clean as www.domain.com/name/2/.
Also i have another question.. I have tried to rewrite the other URLS without luck.
If they write: www.domain.com/page.php?id=name&pg=2 and want to change the address bar URL to be be clean again, but that completely went wrong for me. Is there any specific way to do this by using what I have already made?
Thanks in advance for your help.
EDIT
The solution was based on PHP and not .htaccess. The answer was found based on this question: Stylesheet does not load after using RewriteRule and include . My problem was caused by PHP including relative to the public URL and directory. I have been forced to define a main URL variable to place before any foreign includes.
RewriteCond is only applicable to the very next RewriteRule.
Have your code this way:
RewriteEngine On
RewriteBase /
# If the request is for a valid directory
RewriteCond %{REQUEST_FILENAME} -d [OR]
# If the request is for a valid file
RewriteCond %{REQUEST_FILENAME} -f [OR]
# If the request is for a valid link
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+page\.php\?id=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L]
RewriteRule ^([\w.-]+)/?$ page.php?id=$1 [L,QSA]
RewriteRule ^([\w.-]+)/([\w.-]+)/?$ page.php?id=$1&pg=$2 [L,QSA]
I need an .htaccess file which will do something like this:
When user input www.site.com/some_folder/test it will keep the address as it is and open the corresponding page (test.php)
When user input www.site.com/some_folder/test.php it will remove .php extension to www.site.com/some_folder/test and it will open the corresponding page (test.php)
What I have right now is:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
and it can only fulfill first scenario but not the second. (when I input www.site.com/some_folder/test.php it will still show www.site.com/some_folder/test.php)
What should I change / add in the .htaccess file to fulfill 2nd scenario ?
Any help will be very much appreciated.
Thanks very much.
add the following directives to your .htaccess:
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule (.*)\.php$ /$1 [R=301,L,QSA,NC]
Edit:
if you're in Localhost :
RewriteBase /sitename/
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule (.*)\.php$ $1 [R=301,L,QSA,NC]