3

I get tons of url with "undefined" added at the end of url, consisting about 20% of my pageview. I tried to check the troubling script source and can't find it. Tried some solutions but none of them are working. Lastly now i'm trying to send url with "undefined" string to redirect 204 using htaccess

is this correct?

RewriteRule ^undefined$ - [R=204,NC,L]

do i need to add "last L" flag?

where do i put it?

above wordpress?

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^undefined$ - [R=204,NC,L]
</IfModule>

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

inside wordpress?

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^undefined$ - [R=204,NC,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

or after wordpress?

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^undefined$ - [R=204,NC,L]
</IfModule>

i've looking for this everywhere, and i want to make sure if this is correct by asking on this stack who dealt with this often. thanks a lot!

  • "redirect 204"? Can you please give an example of a URL that contains this "undefined" string. If "undefined" is part of the URL-path (which is what you are testing with RewriteRule) then I'm surprised your server is responding with a valid response (which makes me think it's not in the URL-path)? Btw: ^undefined$ is an exact match. undefined$ will match at the end of the string. – MrWhite Aug 11 '15 at 1:53
  • here's the example: example.com/test/undefined. the real page without "undefined". here's the problem i had, but more severe stackoverflow.com/questions/11017609/… the undefined page goes to 404. this rewrite, where do i have to put it? inside wordpress part of htaccess? – somuch72 Aug 11 '15 at 2:02
5

In order to match "undefined" at the end of the URL-path you need the regex pattern undefined$. The pattern ^undefined$ (which you've used in your question) matches the exact URL "undefined", which is never going to match, unless the request is for http://example.com/undefined.

This directive should go at the top of your .htaccess file (after the RewriteEngine On directive). You only need one RewriteEngine On directive at the top of your script.

So, if you are sure that these requests are coming from your site then you could serve a 204 No Content as you are doing (yes, the L flag is required).

RewriteRule undefined$ - [R=204,NC,L]

In compliant browsers, as far as the user is concerned, literally nothing happens if the user follows such a link (if this really is a link they are clicking on). However, this can obviously be confusing for the user. Also, if this is an external website that is linking to you, then again, nothing happens when the user clicks the link - they don't reach your website.

However, it would be preferable to redirect to the correct URL (if it's simply a case of removing the "undefined" portion from the end of the URL-path). This would catch all situations and inform search engines of the correct URL (reports in the linked articles suggest that Googlebot is also crawling these URLs).

RewriteRule (.*)/undefined$ /$1 [R=301,L]
| improve this answer | |
  • Sorry, it is 204. i put wrong code on question, just edited it. Yes, it will trigger 404, but the undefined page is around 20% of my pageviews, so it's better if it can be stopped. – somuch72 Aug 11 '15 at 2:10
  • 204? A 204 is a success code, not a redirection. (?) Is a 204 something you are seeing or is what you are trying to respond with? R=204 does not make sense. – MrWhite Aug 11 '15 at 2:16
  • I see your point, why i choose 204 because i want when the visitor somehow click the undefined page url, nothing will happen. So the visitor will stay in the current page, going nowhere. – somuch72 Aug 11 '15 at 2:18
  • response 204. sorry, i'm pretty new with this code :D – somuch72 Aug 11 '15 at 2:19
  • Do you really think these are internal links? Do your access logs (HTTP Referer) agree with this? The redirect to the "correct" URL might be the best thing to do (see my update) - since this would seem to be easy to do if it's just a case of removing the "undefined" string. – MrWhite Aug 11 '15 at 2:25
3

If you're getting alot of requests with undefined added to the URLs and you know the things accessing those URLs are people and not robots, you're much better off using HTTP status code 301 and redirecting the URL to the correct one.

Using status code 204 will not help because it means "No content" and the user will then need to manually modify the URL in the address bar to the correct one to access the page. Depending on the browser, a request returning a status codes 204 will either produce a pop-up message indicating the document has no content or the screen will simply be blank.

| improve this answer | |
  • The referer for undefined pages are its real page, so with 204 it will stay at that page. There's no need to redirect to the correct one because it's already on the correct page. I know this is weird, but it's real. I checked on raw access log several time, all undefined page come from firefox on various version. Now i'm using this RedirectMatch 204 (.*)/undefined$ – somuch72 Aug 11 '15 at 3:11
  • 1
    I apologize if you don't like my opinion but its a poor user experience to serve empty pages with an HTTP 204 status code. – Mike -- No longer here Aug 11 '15 at 3:28
  • 1
    It's okay, every opinion is important, i will try 301 as well, i don't know which one will suit better. I'm new with this kind of thing, it should be me that have to apologize, sorry. Thanks! – somuch72 Aug 11 '15 at 3:34
  • "serve empty pages" - In all the browsers I've tested (IE11, Chrome, Opera and Firefox) when the browser receives a 204 No Content, it literally does nothing (as far as the user is concerned). There is no popup and no empty page served. The user just stays where they were, as if they had clicked a blank/inert area of the page. – MrWhite Aug 11 '15 at 11:37
  • that's what I meant by "serve empty pages". and I say serve because the server returned the header to the browser even though we don't see those headers as we browse the web. – Mike -- No longer here Aug 11 '15 at 15:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.