.htaccess remove URL path segments that are causing an Internal Server Error

ramenknight

The following .htaccess Rewrite below seems working fine if the URL is something like: example.com/news/post-1

RewriteRule ^/?news/([^/]+)/?$ article.php?slug=$1 [L,QSA]

However, if the URLs have more parameters, something like: example.com/news/post-1/comment-page-1 (URLs from another version of the website) I will get a 500 Internal Server Error.

How I can make it redirect to the related post instead of 500 Internal Server Error?

MrWhite

if the URLs have more parameters, something like: example.com/news/post-1/comment-page-1 (URLs from another version of the website) I will get a 500 Internal Server Error.

That 500 error is caused by something else in your .htaccess file since the directive you posted would not apply (as stated by @CBroe in comments).

So, the 500 error could potentially be resolved by fixing whatever directive is causing this.

Incidentally, these are "path segments", not "parameters". You might see them as parameters to your script, but that's not what they are in the URL being requested.

How I can make it redirect to the related post instead of 500 Internal Server Error?

If you want to resolve this by redirecting /news/<slug>/<anything> to /news/<slug>/ then you could do something like the following near the top of your .htaccess file, before the existing rewrites:

# Remove additional path segments from the URL-path
RewriteRule ^(news/[^/]+/). /$1 [R=302,L]

The dot on the regex matches a single character after the last slash, so must therefore contain an additional path segment (at least).

The $1 backreference contains just the part of the URL-path you are interested in (ie. /news/<slug>/) and redirects to this.

Note that this is currently a 302 (temporary) redirect. Always test with 302s to avoid potential caching issues before changing to a 301 (permanent) redirect once you have confirmed it works as intended.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

.htaccess Causing Internal / Forbidden Server Error

Apache Clean URL Code Causing An Internal Server Error

Internal Server Error .htaccess

Htaccess rewrite url gives 500 Internal server error

Why is my Apache2 .htaccess file CORS header causing a 500 internal server error?

Internal Server Error - HTACCESS file

imagecreatefromstring is causing 500 internal server error

PDOStatement::fetch causing 500 Internal server error

Cronjob causing 500 internal server error

Currently, calling the connections api with the public-profile-url is causing an internal server error

Conditional redirect in .htaccess depending on the number of path segments in the URL

Nginx rewrite - Remove path segments from URL for static assets?

Parameters in Path Segments of URL

Internal Server Error after changing htaccess

htaccess permanent redirect throws Internal Server Error

.htaccess PHP Controller Internal Server Error

.htaccess/.htpasswd 500 Internal Server Error

Apache - htaccess 500 internal server error

.htaccess throws Internal server error for subdomain

.htaccess Internal Server Error: ErrorDocument not working (RewriteEngine on)

500 internal server error due to htaccess

.htaccess causes Internal Server Error on wamp

.htaccess file not running and internal server error

.htaccess file causes internal server error

internal server error with htaccess rewrite rule

Web Config Error Causing 500 Internal Server Error

Mod rewrite causing internal server error, error 500

Remove path & extension on URL via htaccess

Php file sometimes causing 500 Internal Server Error