Mobile app version of vmapp.org
Login or Join
XinRu657

: Redirect 204 with htaccess if url contain certain string 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

@XinRu657

Posted in: #Apache #Htaccess #ModRewrite

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!

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @XinRu657

2 Comments

Sorted by latest first Latest Oldest Best

 

@LarsenBagley505

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.

10% popularity Vote Up Vote Down


 

@Ann8826881

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 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$ / [R=301,L]

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme