scott
scott

Reputation: 1194

How to setup basic URL Routing

Please forgive me because I don't know the correct keywords for this. How can I make any variation like example.com/page/23920 redirect to example.com/page/?

I am storing IDs passed from my app into URLs in the format: example.com/page/12345

and what I want to do is read the ID value from the URL. I'm new to web development, and I don't understand how get these randomly generated pages setup--right now all the pages don't exist on my server and so they are all 404s.

In my .htaccess file I was trying:

RewriteEngine on
RewriteRule ^page/$1 /page/? [L,R=301]

I still get the error that the resulting page was not found on the server.

I've been reading about "action controllers" but I have no idea how this works.

Upvotes: 1

Views: 33

Answers (1)

nlu
nlu

Reputation: 1963

These are two different questions.

If you want to pass some values to an URL, you should create URLs that look like this, for example:

http://www.example.tld/evaluate_id?id=12345

On your page (that I renamed to "evaluate_id") you have a GET Parameter named "id" with the given value.

However, to redirect any pages below that level to the main page, you should just change your redirect rule as follows:

RewriteRule ^page/[0-9]+ /page/? [L,R=301]

Or to achieve the same result, as mentioned:

RewriteRule ^page/([0-9]+) /page/?id=$1 [L,R=301]

The $-Parameters, are used to reference matching parts of the regular expression on the left side in the redirection part.

Upvotes: 3

Related Questions