I am using mod_write to pull urls out of my database. I have it working at a basic level, but what I am stuck on is creating complex URLs using various slugs pulled from the database as directories in the URL.
Currently the url before using mod_rewrite looks like:
www.domainname.co.uk/company?=1&staff=3
Which takes you to a staff profile within an appropriately styled company page.
But what I want the URL to look like is:
www.domainname.co.uk/COMPANY-NAME/STAFF-NAME
My htaccess currently looks like:
RewriteEngine On
RewriteRule ^c/(.*)$ ./company.php?company=$1
Which results in the URL
www.domainname.co.uk/c/COMPANY-NAME
How do I add the second level directory?
You can use on additional rule for that:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/([\w-]+)/?$ company.php?company=$1&staff=$2 [L,QSA]
RewriteRule ^c/([\w-]+)/?$ company.php?company=$1 [L,QSA,NC]
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-\.]+)/([a-zA-Z0-9_-\.]+)(/?)$ /company.php?company=$1&staff=$2 [NC,L]
That should allow urls of the type:
www.domainname.co.uk/COMPANY-NAME/STAFF-NAME
Related
SEO friendly URL without ht-access in core PHP as per WordPress.
only use with database .
how to create seo friendly url as per wordpress in core php without using .htaccess?
wordpress work on this htaccess
RewriteEngine On
RewriteBase /sevenoutsource/
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /project/index.php [L]
Like this htaccess create on core php
and not want to add any rule like
RewriteRule ^userprofile.html$ /user_profile.php [L]
this url want to store in database and rewrite from that.
You mean making not change to .htaccess and change url type it has one possibly way but it's also have some limit like this if your custom url type http://sitename.com/%postname% just only to making change that %postname% part
http://sangkrit.net/wp-content/uploads/2014/04/sangkrit.net_setting-url-hierarchy-wordpress-post2_thumb.png
on database that table is wp_posts
also check this
https://wordpress.stackexchange.com/questions/26388/how-to-create-custom-url-routes
i know this is a common question but i cannot figure this one out.
I have the following URL:
http://buildsanctuary.com/viewbuild.php?id=3&title=this_is_a_title&page=1
What i wish is the URL to be:
http://buildsanctuary.com/viewbuild/3/this_is_a_title/1
Normally for mod rewrites i would send the user to the link i want and let htaccess do all the work.
So in this case i have tried linking the users to the preferred URL style and rewriting the URL but to no avail.
Any help on how i should be handling this? I want to send the users to the preffered URL but then can i use htaccess to allow me to process the page and URL $_GET information in the same way as the normal dynamic URL?
I have tried the mod rewrite generators etc but nothing works.
This is what the gens have given me:
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.php$ /viewbuild.php?id=$1&title=$2&page=$3 [L]
Thanks.
Fix the generator rule:
RewriteRule ^viewbuild/([^/]*)/([^/]*)/([^/]*)\.php$ /viewbuild.php?id=$1&title=$2&page=$3 [L,QSA]
However, I would do it this way:
RewriteRule ^([^/]*)/(.*) /$1.php/$2 [L]
and then map what you get in $_SERVER['PATH_INFO'] to you preference in PHP.
Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?id=$2&title=$3&page=$4 [L,QSA]
Should do the trick
^viewbuild/([0-9]+)/([a-z_]+)/-([0-9]+)/?$ viewbuild.php?id=$1&title=$2&page=$3 [NC,L]
I have a website for listing education centers.I have two doubts.
I have a URL for printing one college details like this
www.example.com/education/eduS.php?Main=colleges&Name=nameOfCollege
I rwrite this url like this
www.example.com/education/colleges/nameOfCollege
my htaccess is
RewriteRule ^(.*)/(.*)$ eduS.php?Main=$1&Name=$2 [L,NC,QSA]
My problem is that
I want to skip the folder name eduaction from my url so as to get URL like this
www.example.com/colleges/nameOfCollege
How it is possible. I want to get Main and Name as GET parameters.
my Second problem is that , I have another url for listing colleges name in one page. The url like this
www.example.com/education/edu.php?Main=colleges&Category=Engineering-colleges
I rewrite like this
www.example.com/education/colleges/Engineering-colleges
my htaccess
RewriteRule ^(.)/(.)$ edu.php?Main=$1&Category=$2 [L,NC,QSA]
but this shows little bit confusion. The above two URL have same number of GET parameters. How can I handle this problem.
I also need to skip same folder name education from the second URL. Anyone help me
Use this code in your DocumentRoot/.htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ education/eduS.php?Main=$1&Name=$2 [L,QSA]
This allows you to have your URLs like: /colleges/nameOfCollege
I have a page on my website that dynamically displays content. The URL structure is mywebsite.com/giveaway/giveaway.php?id=(any number)
I wish to change that dynamic URL into a static/friendly URL mywebsite.com/giveaway/name-of-giveaway-corresponding-to-id.
In my .htaccess file found in my root folder i have the following:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+\?page=([^\s&]+) [NC]
RewriteRule ^ /page/%1? [R=301,L]
# existing rule
RewriteRule ^page/([^/]+)/?$ /?page=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [L]
The current .htaccess file removes .php and also redirects and changes a URL on my site from
mywebsite.com?page=number to mywebsite.com/page/number
I know you have to get the name corresponding to the ID in php from my database and use that, but im not sure how to write the code in the .htaccess file or how to use and pass the results from the database. Thanks for any help!
I don't know if that is what you mean, but you can't connect the rewrite engine, which is part of the underlying Apache server layer, to the database, which has to be accessed by php.
So you can't rewrite the "name-of-giveaway-corresponding-to-id" to the id directly, you need to rewrite it to something like giveaway.php?nameofgiveaway=(name of giveaway) and then search the database for that string.
Your way to go is to add a rewrite rule like
RewriteRule ^giveaway/([^/]+)$ giveaway.php?nameofgiveaway=$1 [L,QSA]
(not tested, so forgive me if something is wrong) and search for $_GET['nameofgiveaway'].
I am trying to get url from:
192.168.0.1/movie-page.php?id=123
to:
192.168.0.1/movie/movie-name
or even (for now):
192.168.0.1/movie/123
I've simplified it by using this url (to get something working):
192.168.0.1/pet_care_info_07_07_2008.php TO 192.168.0.1/pet-care/
my .htaccess file:
RewriteEngine On
RewriteRule ^pet-care/?$ pet_care_info_07_07_2008.php [NC,L]
What am I doing wrong? I've tried many combinations but no luck and my patience is running out...
I am running this on my local NAS which should have mod_rewrite enabled by default. I have tested .htaccess by entering random string in .htaccess file and opening the page, I got 404 error. I assume this means that .htaccess is being used since the page stops functioning if the file is malformed.
If you want to rewrite:
192.168.0.1/movie-page.php?id=123 too
192.168.0.1/movie/movie-name or 192.168.0.1/movie/123
Then you would do something like, but will require you manually add a rewrite for any new route (fancy url) you want, and eventually you may want your script to create routes dynamically or have a single entry point to sanitize:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^movie/([a-zA-Z0-9-]+)$ movie-page.php?id=$1 [L]
So a better method is to route everything through the rewrite:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]
Then handle the route by splitting the $_GET['route'] with explode()
<?php
//192.168.0.1/movie/movie-name
$route = (isset($_GET['route'])?explode('/',$_GET['route']):null);
if(!empty($route)){
//example
$route[0]; //movie
$route[1]; //movie-name
}
?>
You want something like this:
RewriteRule ^movie/([0-9]*)$ /movie-page.php?id=$1 [L,R=301]
That will give the movie ID version with a numeric ID.