I am looking to create a PHP file that can handle multiple GET variables and output data accordingly, however I would like to remove the base "category" URL element in order to shorten it, with htaccess, like so:
http://website.com/news/politics
http://website.com/videos/funny
http://website.com/posts/food
Currently I use different files for each type, but it is a pain:
RewriteRule ^news/([^/]+)/?$ tpl-news.php?slug=$1 [L,QSA,NC]
RewriteRule ^videos/([^/]+)/?$ tpl-videos.php?slug=$1 [L,QSA,NC]
RewriteRule ^posts/([^/]+)/?$ tpl-posts.php?slug=$1 [L,QSA,NC]
etc ...
Alternatively, I could use the following:
RewriteRule ^category/([^/]+)/?$ tpl-category.php?type=$1&slug=$2 [L,QSA,NC]
I do not want to have a URL like:
http://website.com/category/news/politics
http://website.com/category/videos/funny
http://website.com/category/posts/food
But would like to remove the "category" from the URL like so:
http://website.com/news/politics
http://website.com/videos/funny
http://website.com/posts/food
However other pages / URLs, for example like the ones below, should not be affected by this rule:
http://website.com/account/user
http://website.com/newspost/abcdef // a single post page
http://website.com/videopost/abcdef // a single video page
These "types" (news, videos, posts, ...) are limited in number, i.e. I have no objection predefining those, so that they do not conflict with other URLs of the page (such as image folders, admin sections, user profile sub pages, etc)
You can use this generic rule:
RewriteRule ^(news|videos|post)/([^/]+)/?$ tpl-category.php?type=$1&slug=$2 [L,QSA]
Or if you want to use existing .php files e.g. tpl-news.php, tpl-post.php etc then use:
RewriteRule ^(news|videos|post)/([^/]+)/?$ tpl-$1.php?slug=$2 [L,QSA]
Related
I have a link that looks like this
http://www.example.com/artistmusic?address=JKanyomozi
I would like to change this URL to
http://www.example.com/JKanyomozi/music
I've only been able to do this by adding music as a parameter instead of adding it to the new URL as a prefix.
In short, i've turned this URL ...
http://www.example.com/artistmusic?address=JKanyomozi&req=music
... using this rule ...
RewriteRule ^([^/]*)/([^/]*)$ /artistmusic?address=$1&req=$2 [L]
... into ...
http://www.example.com/JKanyomozi/music
But the problem with this is that for other pages like the one for the videos, this rule redirects to the same page http://www.example.com/JKanyomozi/music instead of http://www.example.com/JKanyomozi/videos
The videos page's original URL is something like this ...
http://www.example.com/artistvideos?address=JKanyomozi
Which rule can I use to change the URL above to
http://www.example.com/JKanyomozi/videos
This applies for other pages like artistevents, artistphotos and artistbiography
Thank you in advance!
Try this :
RewriteRule ^([^/]*)/([^/]*)$ /artist$2?address=$1 [L]
I decided to use mod_rewrite to make my URLs look better. I created very simple rules:
RewriteEngine on
RewriteBase /
RewriteRule ^(profile|contact|help|events|account|logout)/?$ index.php?p=$1 [NC]
RewriteRule ^home/?$ index.php [NC]
RewriteRule ^PlaceOrder/([0-9]+)/?$ index.php?p=mos&gc=$1 [NC]
It almost works well but it doesn't. So for example, [NC] is supposed to make it case-insensitive. However, for some reason, when I type for example localhost/Help in the browser, for some strange reason, it redirects to home page.
Another issue is with the last rule. If I type in localhost/PlaceOrder/1 it works as expected and opens index?p=mos&gc=1. But after that if I click on, for example, Account button, the browser uses this URL: localhost/PlaceOrder/account. Which is wrong. It should only use localhost/account. Why is it adding that sub-directory in there? It is only happening after the last rule is used. Prior to using the last rule, all links work well.
I'm going to guess that the localhost/Help isn't because of the rules and because of something in your index.php script. There's nothing those rules do that could possibly rewrite Help to the home page.
The second issue is a matter of a relative vs absolute URL issue. You're probably using relative links in your pages and because you've changed your relative URL base (which would be /PlaceOrder/ all relative links will have that prepended to it. You need to either change all your links to absolute URLs (they'd start with a /) or add this to your page's header:
<base href="/" />
I am trying to rewrite the urls of a site, i should mention that the way index.php works now is getting the p (page) parameter and including the appropriate file.
So requesting a page is like this:
www.domain.com/index.php?p=home
www.domain.com/index.php?p=search
www.domain.com/index.php?p=profile
www.domain.com/index.php?p=contact
I found how to create a rewrite rule for this:
RewriteRule ^([A-Za-z0-9-]+)/?$ index.php?p=$1
so now www.domain.com/home would give me the home page
But i also need to have a friendly url for this
www.domain.com/index.php?p=profile&id=20
to
www.domain.com/profile/20/profile-friendly-name
or better
www.domain.com/profile/profile-friendly-name
*The profile-friendly-name refers to a company name
Requirements:
To have friendly urls for all pages e.g. /home, /contact
To have a particular friendly url for the profile page with the
profile name in the url
My questions:
How can i add a profile-friendly-name to the url with the existing url format (index.php?p=profile&id=20)?
Can i only have a unique name there (without the id), like my last
example?
If i manage to do that, will i have to change ALL the existing urls
within the site (links, urls to images, to files) to the friendly
format?
I noticed that after applying the first RewriteRule some css
stylesheets and js are not included. What is wrong?
RewriteRule ^profile/([0-9]+)/([A-Za-z0-9-]+)/?$ index.php?p=profile&id=$1
Should work for :
www.domain.com/index.php?p=profile&id=20
to
www.domain.com/profile/20/profile-friendly-name
Ex 1 :
https://example.com/books.php?bookName=php using htaccess url will be written like this https://example.com/php
You can do this using code given below in .htaccess file:
RewriteEngine On
RewriteRule ^([^/\.]+)/?$ books.php?bookName=$1
Ex 2 :
https://example.com/books.php?bookName=php&&chapter=array using htaccess url will be written like
https://example.com/php/array
You can get by using code in .htaccess file given below:
RewriteEngine On
RewriteRule ^([^/\.]+)/([^/\.]+)?$ books.php?bookName=$1&chapter=$2
I'm creating a new website and it has some php in it. The site basically would work like this, i have /index.php?page=category_page. The the category would be the category and the page would be the sort-of sub category / actual page. The rewritten rule would look like this: /category/page.
I've got this:
RewriteEngine On
RewriteRule ^category/([^/]*)$ index.php?page=$1 [L]
But i dont know how to separate the category and the page, any help?
The second thing is, in my index.php (for instance) i have some css from an external file:
<link rel="stylesheet" type="text/css" href="style/style.css"/>
This doesn't work with the rewrite rule, because it tries to load /category/page/style/style.css which doesn't exist / doesn't rewrite. How do i make it work? I know a simple fix would be to put /style/style.css and that would load from the root of the website, but i'm currently running the website from a sub directory e.g. example.com/new/index.php so that isn't an option. Any help with this?
Problem 1:
You could define multiple parameters in your regex, e.g. category & page (see below).
With such a broad rewrite rule, you would want to add a condition not to rewrite for stylesheets, images, and other assets, though.
I also modified your pattern to only match letters, digits, hyphens, and underscores, which would prevent the use of non-standard characters in your category or page names.
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(js|css|png|gif|jpg)$ [NC]
RewriteRule ^([\w\d\-]+)/([\w\d\-]+)/?$ index.php?category=$1&page=$2 [L]
Problem 2:
You're using a relative url in your href, which is appending the stylesheet's location to the current location defined in your browser (/category/page/).
Even though the server-side is rewriting that URL, the browser is unaware of the rewrite.
If you use an absolute URL instead, your browser will define the URL relative to the BASE url (/).
Try this:
<link rel="stylesheet" type="text/css" href="/style/style.css"/>
Use this RewriteRule
RewriteEngine On
RewriteRule ^(.+)/(.+)/$ index.php?page=$1_$2 [L]
I want to make my URL clean. Below is what I want to do:
Dirty URL: www.site.com/index.php?page=products
Clean URL: www.site.com/products/
For this, I write these codes in .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]*)/$ /index.php?page=$1
But whenever I click on the links in my project, I redirect to localhost page. Links are in this form:
Products
I think it may be related to href value.
What's the correct form for href value? href="product",href="/product/",etc. ?
There is no correct form for your href attribute, each form acts in different ways:
Products
The above would go relatively from the page you are currently on, so might be domain.com/products but might be domain.com/shop/products
You are better using a link direct from the root by using a / at the start, for example:
Products
would go to domain.com/products (note the lack of a / at the end of the url, which will make it not work with your current htaccess rule)
To make both /products and /products/ go to the correct place, change your htaccess rule to:
RewriteRule ^([^/]*)/?$ /index.php?page=$1
The ? will make the character preceding it optional, so the / in this case.