controller and method translate in htaccess mod rewrite - php

I have some URL:
index.php?controller=user&method=show&id=7
in this URL: controller,method and show, are my keys
user, show and 7 are some kind of value
I would like translate URL to:
index.php?user/show/7
Is there any posibilities to translate this URL in .htaccess file?
Now, my .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /Cobbler/index.php

I don't see the benefit of this but you can replace your current code by this one
RewriteEngine on
RewriteBase /Cobbler/
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule . ./ [R=301,L,QSA]
RewriteCond %{QUERY_STRING} ^([^/]+)/([^/]+)/([^/]+)$
RewriteRule ^$ index.php?controller=%1&method=%2&id=%3 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]

Related

Friendly URL with two properties?

I am trying to rewrite an ugly URL that's using two properties. For example, I want example.com/stories/?url=how-are-you?num=5 to be example.com/stories/how-are-you/5 I have rewritten the first property, but I don't know how to rewrite the num property. Here's my .htaccess file:
RewriteEngine On
RewriteBase /stories/
RewriteCond %{THE_REQUEST} /\?url=([^&\s]+) [NC]
RewriteRule ^ %1? [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?url=$1 [L,QSA]
Try :
RewriteEngine On
RewriteBase /stories/
RewriteCond %{THE_REQUEST} /\?url=([^&\s]+)&num=([0-9]+) [NC]
RewriteRule ^ %1/%2? [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?url=$1&num=$2 [L,QSA]

.htaccess to search events with a nice looking url

I'd like code for a .htaccess file that when this is entered:
domain.com/event/granniesteaparty a file in the folder 'event' (display.php) takes the 'granniesteaparty' bit as though domain.com/event/display.php?search=granniesteaparty had been entered.
If tried all sorts of things but this is where I am at the moment:
RewriteEngine on
RewriteRule ^event/([A-Za-z0-9]+)/([0-9]+)/?$ /event/display.php?search=$1 [NC,L]
RewriteCond %{QUERY_STRING} s=([^&]+)
RewriteRule ^event /event/1? [NC,R=301]
I hope that makes sense, could someone point me in the right direction please?
You have too many capture groups in your first rule. Give this a try
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{THE_REQUEST} ^GET\ /+event/display\.php\?search=([^&\s]+)
RewriteRule ^ /event/%1? [R=301,L]
RewriteRule ^event/(\w+)/?$ /event/display.php?search=$1 [NC,L]
Put the following code .htaccess inside event directory:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} !\s/+event/display.php?search= [NC]
RewriteRule ^(.*)$ event/display.php?search=$1 [R=302,L,NE]
RewriteRule ^event/display.php?search=$1(.*)$ /$1 [L,NC]
Update
If you want only to redirect any wrong page to the display page but internally so the following code will do it :
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*) event/display.php [L,NC]
So when someone request /event/whateverwrong it will be at the url as it is but internally will map to display.php

htaccess rewrite rule for subfolder (nice url)

I have trying to figure out how to get working in subfolder "admin" the same thing which works in root dir, problem is I don't know how to do that.
Currently I have:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ?arg=$1 [L,QSA]
# /
RewriteCond %{REQUEST_URI} !\.[[:alnum:]]+$
RewriteRule ^(.+[^/])$ /$1/ [R=301]
Now it works like:
http://example.com/page translate into http://example.com/?arg=page
What I would like to do in addition is this:
http://example.com/admin/page translate into http://example.com/admin/?arg=page
I don't know at all how to make that happen, can somebody help me please?
Thanks
You can use these rules with appropriate RewriteBase:
RewriteEngine On
RewriteBase /admin/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ?arg=$1 [L,QSA]
Or just add
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/admin/(.*)$ admin/index.php?arg=$1 [L,QSA]

PHP .htaccess exception some of .php file

I have .htaccess that the function is to hide .php extension and convert user.php?user=username to be :
http://example.com/hidayurie
Now I have a problem, I have file search.php. When I run the URL : http://example.com/search?q=username. It still detect that search is username whereas I have file search.php
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule (.*) http://example.com/$1 [R=301,L]
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?user=$1
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ user.php?user=$1&page=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ user.php?user=$1&page=$2
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/home/.*$ index.php [QSA]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
How can I set if .php file, then it will open that php file without extension?
Try adding some conditions to your rules to prevent the rewrite if the URI points to a file that exists. You should also make sure you have multiviews turned off (this can be anywhere in your htaccess file):
Options -Multiviews
Then add these conditions before each of these 4 rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?user=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?user=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ user.php?user=$1&page=$2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ user.php?user=$1&page=$2
You need to define search and any other different pages/URLs you wish to rewrite, otherwise yeah it will continue to think it's a user and refer to user.php
Example:
RewriteRule ^search/?$ /search.php
RewriteRule ^search/([a-zA-Z0-9_-]+)/?$ /search.php.php?q=$1
Second line would allow you to query your search with a clean url, ex; yoursite.com/search/username/

url rewriting while url creates dynamically

I had rewrite a URL from example.com/service.php?p_id=1&s_id=seo to example.com/1/seo/.
By The following rule:
this rule is for example.com/services.php?p_id=1&s_id=seo
RewriteEngine On
RewriteCond %{THE_REQUEST} /services\.php\?p_id=([^&\s]+)&s_id=([^&\s]+) [NC]
RewriteRule ^ /%1/%2/? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ services.php?p_id=$1&s_id=$2 [QSA,L]
Now the problem is that when i use the following rule for another url like example.com/about-us.php?p_id=1&s_id=2 and for example.com/it-training.php?p_id=1&s_id=2 then the url redirects as example.com/1/2/index.php but it should be as example.com/1/2 ,
formated rule for about-us.php is given bellow
RewriteEngine On
RewriteCond %{THE_REQUEST} /about-us\.php\?p_id=([^&\s]+)&s_id=([^&\s]+) [NC]
RewriteRule ^ /%1/%2/? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ about-us.php?p_id=$1&s_id=$2 [QSA,L]
In other case i have some url like example.com/packages.php, index.php, i also want to remove the .php from that kind of files

Categories