-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.vue
245 lines (207 loc) · 6.41 KB
/
app.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<template>
<main>
<h1>301 Redirect Tool</h1>
<!-- prettier-ignore -->
<p>Create free 301 redirects for your websites. <mark>100% free</mark>, <em>no login required</em> & <a href="https://github.com/timb-103/301-redirect" target="_blank">open source</a>.</p>
<!-- Create Form -->
<form @submit.prevent="create()" v-if="!showSearch && !redirect" class="notice">
<div>
<label>1. Enter a name</label>
<input
type="text"
v-model="subdomain"
placeholder="Enter a subdomain (aka name), eg: acme"
style="width: 400px"
/>
</div>
<div>
<label>2. Redirect to URL</label>
<input
type="text"
v-model="url"
placeholder="Enter a URL to redirect to, eg: https://acme.com"
style="width: 450px"
/>
</div>
<!-- Errors -->
<div v-if="errors">
<code>{{ errors }}</code>
</div>
<!-- Submit Button -->
<button type="submit" :disabled="loading">Create Redirect</button>
<!-- Open Search Link -->
<div>
<a href="/?redirect=" @click.prevent="showSearch = true">Looking for your redirect?</a>
</div>
</form>
<!-- Search -->
<div v-if="showSearch && !redirect" class="notice">
<form @submit.prevent="search()">
<!-- Search Input -->
<div>
<input
type="text"
v-model="searchQuery"
placeholder="Enter your redirect subdomain/name, eg. acme"
style="width: 450px"
/>
</div>
<!-- Search Errors -->
<div v-if="searchErrors">
<code>{{ searchErrors }}</code>
</div>
<!-- Search Button -->
<button type="submit" :disabled="loading">Search</button>
</form>
<!-- Back Button -->
<div>
<a href="" @click.prevent="clear()">Go Back</a>
</div>
</div>
<!-- Success -->
<div v-if="redirect" class="notice">
<p><strong>Your redirect:</strong></p>
<p>
<!-- prettier-ignore -->
<code><strong>https://{{ redirect.subdomain }}.301redirect.to</strong> -> <strong>{{ redirect.url }}</strong></code>
</p>
<p><strong>How does it work?</strong></p>
<ol>
<li>
Add a <strong>CNAME</strong> record, with a <strong>host</strong> of <code>@</code> and
<strong>value</strong> of <code>{{ redirect.subdomain }}.301redirect.to</code> in your DNS settings via your
domain registrar.
</li>
<li>Wait for it to propogate, it can take up to 24 hours but usually much faster.</li>
</ol>
<button @click="clear()">Go Back</button>
</div>
<!-- How it works -->
<p><strong>Why use this 301 redirect tool?</strong></p>
<ul>
<li>✅ <strong>100% free</strong>, no login required</li>
<li>💸 No need to host a server</li>
<li>🤖 Great for <strong>SEO</strong></li>
</ul>
<p>
Over the years I've acquired many websites and
<a href="https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/301" target="_blank">
<strong>301 redirected</strong>
</a>
them to another website. Each time this requires setting up a server to host a simple redirect server to point the
traffic somewhere else.
</p>
<p>This means for each website I want to redirect, I need to host a server, which costs money.</p>
<p><strong>This tool will do the redirect for you.</strong></p>
<p>
All you do is add a
<a href="https://en.wikipedia.org/wiki/CNAME_record" target="_blank">
<strong>CNAME</strong>
</a>
record in your domain registrar pointing to the subdomain you choose, and we'll automatically 301 redirect it for
you.
</p>
<p>No need to host anything, and it's completely free!</p>
</main>
</template>
<script setup lang="ts">
import { Redirect } from 'types/redirect'
// add page meta
useSeoMeta({ title: '301 Redirect Tool' })
const loading = ref(false)
const verified = ref(false)
const subdomain = ref('')
const url = ref('')
const errors = ref('')
const searchErrors = ref('')
const redirect = ref<Redirect | null>()
const showSearch = ref(false)
const searchQuery = ref('')
async function create() {
errors.value = ''
redirect.value = null
navigateTo('/')
if (!url.value || !subdomain.value) {
errors.value = 'Please enter a subdomain & URL.'
return
}
if (!isValidSubdomain(subdomain.value)) {
errors.value = 'Only characters, numbers, and hyphens are allowed in your subdomain.'
return
}
if (!isValidURL(url.value)) {
errors.value = 'URL must start with https://'
return
}
loading.value = true
// add plausible event
useTrackEvent('createRedirect')
try {
const response = await $fetch<Redirect>('/api/redirects/create', {
method: 'POST',
body: {
subdomain: subdomain.value.toLowerCase(),
url: url.value.toLowerCase(),
},
})
redirect.value = response
} catch (error: any) {
errors.value = error.statusMessage || error.message
}
loading.value = false
}
async function get(name: string) {
loading.value = true
errors.value = ''
try {
const response = await $fetch<Redirect>('/api/redirects/get', {
method: 'POST',
body: {
subdomain: name,
},
})
redirect.value = response
} catch (error: any) {
errors.value = error.statusMessage || error.message
}
loading.value = false
}
async function search() {
loading.value = true
searchErrors.value = ''
try {
const response = await $fetch<Redirect>('/api/redirects/get', {
method: 'POST',
body: {
subdomain: searchQuery.value,
},
})
redirect.value = response
navigateTo(`/?redirect=${response.subdomain}`)
} catch (error: any) {
searchErrors.value = error.statusMessage || error.message
redirect.value = null
navigateTo('/')
}
loading.value = false
}
// Regular expression to match only characters, numbers, and hyphens
function isValidSubdomain(input: string): boolean {
const regex = /^[a-zA-Z0-9-]*$/
return regex.test(input)
}
function isValidURL(input: string): boolean {
return input.includes('https://')
}
function clear() {
redirect.value = null
showSearch.value = false
verified.value = false
navigateTo('/')
}
onMounted(() => {
if (useRoute().query?.redirect) {
get(String(useRoute().query?.redirect))
}
})
</script>