Nikk
Nikk

Reputation: 7921

.htaccess rewrite returning Error 404

RewriteEngine on
RewriteCond %{QUERY_STRING} (^|&)public_url=([^&]+)($|&)
RewriteRule ^process\.php$ /api/%2/? [L,R=301]

Where domain.tld/app/process.php?public_url=abcd1234 is the actual location of the script.

But I am trying to get .htaccess to make the URL like this: domain.tld/app/api/acbd1234. Essentially hides the process.php script and the get query ?public_url.

However the script above is returning error 404 not found.

Upvotes: 0

Views: 400

Answers (3)

Pankaj Gupta
Pankaj Gupta

Reputation: 1

.htaccess rewrite returning Error 404 add this line your .htaccess make sure change domain name Redirect 301 /login http://test.test.com

Upvotes: 0

arkascha
arkascha

Reputation: 42974

I think this is what you are actually looking for:

RewriteEngine on

RewriteCond %{QUERY_STRING} (?:^|&)public_url=([^&]+)(?:$|&)
RewriteRule ^/?app/process\.php$ /app/api/%1 [R=301,QSD]

RewriteRule ^/?app/api/([^/]+)/?$ /app/process.php?public_url=$1 [END]

If you receive an internal server error (http status 500) for that then check your http servers error log file. Chances are that you operate a very old version of the apache http server, you may have to replace the [END] flag with the [L] flag which probably will work just fine in this scenario.


And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files (.htaccess style files). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only supported as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).


UPDATE:

Based on your many questions in the comments below (we see again how important it is to be precise in the question itself ;-) ) I add this variant implementing a different handling of path components:

RewriteEngine on

RewriteCond %{QUERY_STRING} (?:^|&)public_url=([^&]+)(?:$|&)
RewriteRule ^/?app/process\.php$ /api/%1 [R=301,QSD]

RewriteRule ^/?api/([^/]+)/?$ /app/process.php?public_url=$1 [END]

Upvotes: 2

MrWhite
MrWhite

Reputation: 45948

I am trying to get .htaccess to make the URL like this: example.com/app/api/acbd1234.

You don't do this in .htaccess. You change the URL in your application and then rewrite the new URL to the actual/old URL. (You only need to redirect this, if the old URLs have been indexed by search engines - but you need to watch for redirect loops.)

So, change the URL in your application to /app/api/acbd1234 and then rewrite this in .htaccess (which I assume in in your /app subdirectory). For example:

RewriteEngine On

# Rewrite new URL back to old
RewriteRule ^api/([^/]+)$ process.php?public_url=$1 [L]

You included a trailing slash in your earlier directive, but you omitted this in your example URL, so I've omitted it here also.

If you then need to also redirect the old URL for the sake of SEO, then you can implement a redirect before the internal rewrite:

RewriteEngine On

# Redirect old URL to new (if request by search engines or external links)
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} (?:^|&)public_url=([^&]+)(?:$|&)
RewriteRule ^process\.php$ /app/api/%1? [R=302,L]

# Rewrite new URL back to old
RewriteRule ^api/([^/]+)$ process.php?public_url=$1 [L]

The check against REDIRECT_STATUS is to avoid a rewrite loop. ?: inside the parenthesised subpattern avoids the group being captured as a backreference.

Change the 302 (temporary) to 301 (permanent) only when you are sure it's working OK, to avoid erroneous redirects being cached by the browser.

Upvotes: 1

Related Questions