src/pages/ directory.
Navigating Between Pages
Astro uses standard HTML<a> elements to navigate between routes. There is no framework-specific <Link> component provided.
src/pages/index.astro
Static Routes
.astro page components as well as Markdown and MDX Files (.md, .mdx) within the src/pages/ directory automatically become pages on your website. Each page’s route corresponds to its path and filename within the src/pages/ directory.
Dynamic Routes
An Astro page file can specify dynamic route parameters in its filename to generate multiple, matching pages. For example,src/pages/authors/[author].astro generates a bio page for every author on your blog. author becomes a parameter that you can access from inside the page.
In Astro’s default static output mode, these pages are generated at build time, and so you must predetermine the list of authors that get a corresponding file. In SSR mode, a page will be generated on request for any route that matches.
Static (SSG) Mode
Because all routes must be determined at build time, a dynamic route must export agetStaticPaths() that returns an array of objects with a params property. Each of these objects will generate a corresponding route.
[dog].astro defines the dynamic dog parameter in its filename, so the objects returned by getStaticPaths() must include dog in their params. The page can then access this parameter using Astro.params.
src/pages/dogs/[dog].astro
/dogs/clifford, /dogs/rover, and /dogs/spot, each displaying the corresponding dog name.
The filename can include multiple parameters, which must all be included in the params objects in getStaticPaths():
src/pages/[lang]-[version]/info.astro
/en-v1/info and /fr-v2/info.
Parameters can be included in separate parts of the path. For example, the file src/pages/[lang]/[version]/info.astro with the same getStaticPaths() above will generate the routes /en/v1/info and /fr/v2/info.
Rest Parameters
If you need more flexibility in your URL routing, you can use a rest parameter ([...path]) in your .astro filename to match file paths of any depth:
src/pages/sequences/[...path].astro
/sequences/one/two/three, /sequences/four, and /sequences. (Setting the rest parameter to undefined allows it to match the top level page.)
Rest parameters can be used with other named parameters. For example, GitHub’s file viewer can be represented with the following dynamic route:
/withastro/astro/tree/main/docs/public/favicon.svg would be split into the following named parameters:
Example: Dynamic Pages at Multiple Levels
In the following example, a rest parameter ([...slug]) and the props feature of getStaticPaths() generate pages for slugs of different depths.
src/pages/[...slug].astro
On-demand Dynamic Routes
For on-demand rendering with an adapter, dynamic routes are defined the same way: include[param] or [...path] brackets in your file names to match arbitrary strings or paths. But because the routes are no longer built ahead of time, the page will be served to any matching route. Since these are not “static” routes, getStaticPaths should not be used.
src/pages/resources/[resource]/[id].astro
resource and id: resources/users/1, resources/colors/blue, etc.
Modifying the Example for SSR
Because SSR pages can’t usegetStaticPaths(), they can’t receive props. The previous example can be adapted for SSR mode by looking up the value of the slug param in an object. If the route is at the root (”/”), the slug param will be undefined. If the value doesn’t exist in the object, we redirect to a 404 page.
src/pages/[...slug].astro
Redirects
Sometimes you will need to redirect your readers to a new page, either permanently because your site structure has changed or in response to an action such as logging in to an authenticated route. You can define rules to redirect users to permanently-moved pages in your Astro config. Or, redirect users dynamically as they use your site.Configured Redirects
You can specify a mapping of permanent redirects in your Astro config with theredirects value.
For internal redirects, this is a mapping of an old route path to the new route. As of Astro v5.2.0, it is also possible to redirect to external URLs:
astro.config.mjs
status code in addition to the new destination:
astro.config.mjs
Dynamic Redirects
On theAstro global, the Astro.redirect method allows you to redirect to another page dynamically. You might do this after checking if the user is logged in by getting their session from a cookie.
src/pages/account.astro
Rewrites
A rewrite allows you to serve a different route without redirecting the browser to a different page. The browser will show the original address in the URL bar, but will instead display the content of the URL provided toAstro.rewrite().
Rewrites can be useful for showing the same content at multiple paths (e.g. /products/shoes/men/ and /products/men/shoes/) without needing to maintain two different source files.
Rewrites are also useful for SEO purposes and user experience. They allow you to display content that otherwise would require redirecting your visitor to a different page or would return a 404 status. One common use of rewrites is to show the same localized content for different variants of a language.
The following example uses a rewrite to render the /es/ version of a page when the /es-CU/ (Cuban Spanish) URL path is visited. When a visitor navigates to the URL /es-cu/articles/introduction, Astro will render the content generated by the file src/pages/es/articles/introduction.astro.
src/pages/es-cu/articles/introduction.astro
context.rewrite() in your endpoint files to reroute to a different page:
src/pages/api.js
Route Priority Order
It’s possible for multiple defined routes to attempt to build the same URL path. For example, all of these routes could build/posts/create:
- Routes with more path segments will take precedence over less specific routes. In the example above, all routes under
/posts/take precedence over/[...slug].astroat the root - Static routes without path parameters will take precedence over dynamic routes. E.g.
/posts/create.astrotakes precedence over all the other routes in the example - Dynamic routes using named parameters take precedence over rest parameters. E.g.
/posts/[page].astrotakes precedence over/posts/[...slug].astro - Pre-rendered dynamic routes take precedence over server dynamic routes
- Endpoints take precedence over pages
- File-based routes take precedence over redirects
- If none of the rules above decide the order, routes are sorted alphabetically based on the default locale of your Node installation
pages/posts/create.astro- Will build only/posts/createpages/posts/[pid].ts- Will build/posts/abc,/posts/xyz, etc. But not/posts/createpages/posts/[page].astro- Will build/posts/1,/posts/2, etc. But not/posts/create,/posts/abcnor/posts/xyzpages/posts/[...slug].astro- Will build/posts/1/2,/posts/a/b/c, etc. But not/posts/create,/posts/1,/posts/abc, etc.pages/[...slug].astro- Will build/abc,/xyz,/abc/xyz, etc. But not/posts/create,/posts/1,/posts/abc, etc.
Pagination
Astro supports built-in pagination for large collections of data that need to be split into multiple pages. Astro will generate common pagination properties, including previous/next page URLs, total number of pages, and more. Paginated route names should use the same[bracket] syntax as a standard dynamic route. For instance, the file name /astronauts/[page].astro will generate routes for /astronauts/1, /astronauts/2, etc, where [page] is the generated page number.
You can use the paginate() function to generate these pages for an array of values like so:
src/pages/astronauts/[page].astro
/astronauts/1- Page 1: Displays “Neil Armstrong” and “Buzz Aldrin”/astronauts/2- Page 2: Displays “Sally Ride” and “John Glenn”
The page Prop
When you use the paginate() function, each page will be passed its data via a page prop. The page prop has many useful properties:
data- Array containing the page’s slice of data that you passed to thepaginate()functionstart- The count of the first item on the page, starting from 0end- The count of the last item on the page, starting from 0total- Total number of resultscurrentPage- The current page number, starting from 1size- Number of items per page (default: 10)lastPage- Number of last pageurl.current- URL of the current pageurl.prev- URL of the previous page (if there is one)url.next- URL of the next page (if there is one)url.first- URL of the first page (if the current page is not the first page)url.last- URL of the last page (if the current page in not the last page)
Excluding Pages
You can exclude pages or directories withinsrc/pages from being built by prefixing their names with an underscore (_). Files with the _ prefix won’t be recognized by the router and won’t be placed into the dist/ directory.
You can use this to temporarily disable pages, and also to put tests, utilities, and components in the same folder as their related pages.
In this example, only src/pages/index.astro and src/pages/projects/project1.md will be built as page routes and HTML files.