URL rewrite skipping folder name using htaccess - php

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

Related

htaccess show category, sub category and post in url

I'm currently building out a simple site but struggling with a bit of htaccess.
I want my links such as services, about, blog etc to be categories.
I then want to have sub categories from then such as ppc for services, meet the team for about and a selection of blog posts for blog.
When you click on the link such as a post within ppc you get the following URL structure:
sitename/category-name/subcategory-name/post-name
sitename/services/ppc/ppc-management
So far I have been able to get the url to show like this:
sitename/category-name/subcategory-name
or
sitename/category-name/post-name
The two success examples above show that I can get it to show category name and subcategory name together.
Or I can get category-name and post-name together.
So i am struggling to get category-name/subcategory-name/post-name
My current htaccess file looks like this:
RewriteEngine On
RewriteBase /
RewriteRule ^services/(.*)$ catpost.php?id=$1 [L]
RewriteRule ^a-(.*)-(.*)$ archives.php?month=$1&year=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ viewpost.php?id=$1 [QSA,L]
I'm struggling with fixing these two issues:
1) Having a dynamic url insertion before the catpost.php
-You will see in the htaccess i have fixed the the catpost rewrite rule to have services before. However, is there a way to have that change dynamically?
So that could reference to services, about etc.
2) Keeping the subcategory-name in the url when you view the post as it directs to sitename/category-name/post-name
Any help is greatly appreciate. I hope this post is clear but if it isn't please let me know so I can improve.
Well, if I were you, I'll try to deal with all these in a index.php and refer to other pages by include().
Seems that (.*)/(.*) is not allowed as the FIRST rule. If you want to rewrite services/1 to services.php?id=1 and catpost/5 to catpost.php?id=5, try this:
RewriteRule ^a-(.*)-(.*)$ archives.php?month=$1&year=$2 [L]
RewriteRule ^([^/]+)/([^/]*)$ $1.php?id=$2 [NC,L]
[^/] stands for any char except a slash
To finish your secont task together, I tried:
RewriteRule ^([^/]+)/[^/]+/([^/]*)$ $1.php?id=$2 [NC,L]
(It doesn't look pretty. My fault.)
But it requires you to put something between the two slashes, like catpost/something/2, and seems that you'll get a 500 if you leave it empty.

Adding more directories to url using mod_rewrite

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

Mod rewrite and dynamic URLs

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]

mod_rewrite with name replacing ID

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'].

Rewrite URL Parameter containing dot

I think that I am trying to achieve an impossible result.
The scenario is PURL-Mailing and I already got some URL's rewritten to fit the URL, sent to the customer.
The customer enters the site by the following domain: http://domain.com/UserName
The Variable UserName represents the GET-Variable, which equivalent to http://domain.com/index.php?user=UserName
I achieve this with the following rewrite Rules:
RewriteBase /
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?name=$1 [QSA]
#This works perfect and translates to http://domain.com/UserName
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?name=$1 [L]
#This achieves the goal but does not reflect in the URI I want:
#http://domain.com/UserName
To go further, there are also some Names containing a dot in the Name like A.Jackson that also need to be treated as UserName. As those are only 13 Name I could implement them manually. What I don't know is how I can prevent the part after the dot to be handled as a file extension. Is there a way to write a custom handle in *mod_rewrite* for those?
And if so, can anybody explain to me how?
Thanks in advance and best regards!
ok try below
RewriteCond %{REQUEST_FILENAME} !(img|anyother folders that you want to ignore|anyother folders that you want to ignore|...)
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9_-]*[\.]*[a-zA-Z0-9_-]+)[/]*$ test.php?name=$1 [L]
replace 'anyother folders that you want to ignore' with folder name that you want to ignore. Seperate each folders with '|'
You also have to provide full path to the CSS, image or any other links used in your web page when you using URL rewrite functions
Here is your fix
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9_-]*[\.]*[a-zA-Z0-9_-]+)$ index.php?name=$1 [L]

Categories