How to redirect the page after rewrite the URL - apache

I have to rewrite the URL so I have used the below code in my htaccess which is working
RewriteRule ^products/part/pumps/?$ pumps.php [L,NC]
if someone tries to access the url link exmple.com/products/part/pumps/ then it's showing the pumps.php data.
Now my issue is if some try to access the page example.com/pumps.php then how can I redirect to this link exmple.com/products/part/pumps/
I have tried but getting a redirecting error
Redirect 301 /pumps.php /products/part/pumps/
I have many pages and i have to redirect them also. sharing two example here
RewriteRule ^power\.php$ /products/engine/power/ [R=301,L]
RewriteRule ^connecting\.php$ /products/connection/power/connecting/ [R=301,L]

Use the following before your existing rewrite:
# External redirect
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^pumps\.php$ /products/part/pumps/ [R=301,L]
(But test first with a 302 redirect to avoid potential caching issues.)
By checking that the REDIRECT_STATUS environment variable is "empty" we can redirect only direct requests from the user and not the rewritten request by the later rewrite. After the request is successfully rewritten, REDIRECT_STATUS has the value 200 (as in 200 OK HTTP status).
The RewriteCond (condtion) directive must precede every RewriteRule directive that triggers an external redirect.
The Redirect directive (part of mod_alias, not mod_rewrite) is processed unconditionally and will end up redirecting the rewritten request, resulting in a redirect loop. You need to use mod_rewrite throughout.
Use the END flag instead of RewriteCond (requires Apache 2.4)
Alternatively, you can modify your existing rewrite to use the END flag (instead of L) to prevent a loop by the rewrite engine. The RewriteCond directive as mentioned above can then be omitted. But note that the END flag is only available on Apache 2.4+.
For example:
# External redirects
RewriteRule ^pumps\.php$ /products/part/pumps/ [R=301,L]
# Internal rewrites
RewriteRule ^products/part/pumps/?$ pumps.php [END,NC]
It is advisable to group all the external redirects together, before the internal rewrites.
Unfortunately, due to the varying nature of these rewrites it doesn't look like the rules can be reduced further.

Related

.htaccess redirect not redirecting

I am trying to Redirect pages to new location on the same website using .htaccess
the physical file name is displayitems.php but there is a rule in .htaccess
RewriteRule ^buy-online-(.*) ./displayitems.php?url=$1
which is to handle the user friends URLs and works well.
Now i want to redirects these user friendly urls to new location which is on the same website for eg.
redirect https://example.com/buy-online-alhabib-rings4-sku-1658906163 https://example.com/products/jewelry/buy-online-alhabib-rings4-sku-1658906163 [R=301]
redirect https://example.com/buy-online-alhabib-rings3-sku-1658906162 https://example.com/products/jewelry/buy-online-alhabib-rings3-sku-1658906162 [R=301]
redirect https://example.com/buy-online-alhabib-rings2-sku-1658906161 https://example.com/products/jewelry/buy-online-alhabib-rings2-sku-1658906161 [R=301]
redirect https://example.com/buy-online-alhabib-rings1-sku-1658906160 https://example.com/products/jewelry/buy-online-alhabib-rings1-sku-1658906160 [R=301]
these user friendly url doesn't have any extensions like ".php" ".htm" etc
but nothing happening.
I have added this code in php file to check if url doesn't contain \products\ than redirect it to new location with the same name, for testing i just redirect it with 302 once all tested i will change it to 301
if (strpos($_SERVER['REQUEST_URI'], "/products/") === false) { $NewAddress = strtolower("Location:". $ini['website_address_https'] . "products/".$Product['categoriesname']."/".$Product['BrandName'].$_SERVER['REQUEST_URI']); header("$NewAddress",TRUE,302); }
redirect https://example.com/buy-online-alhabib-rings4-sku-1658906163 https://example.com/products/jewelry/buy-online-alhabib-rings4-sku-1658906163 [R=301]
There are 3 main issues here:
The mod_alias Redirect directive takes a root-relative URL-path (starting with a slash) as the source URL, not an absolute URL. So the above will never match.
You have mixed syntax with mod_rewrite. [R=301] is a RewriteRule (mod_rewrite) flags argument and has nothing to do with the mod_alias Redirect directive. Redirect takes the HTTP status code as an optional second argument. eg. Redirect 301 /buy-online-alhabib-rings4-sku-1658906163 ...
Since you are using mod_rewrite (ie. RewriteRule) for your internal rewrite, you should use mod_rewrite for external redirects as well to avoid potential conflicts. These redirects then need to go before your internal rewrite.
Additionally,
In the 4 redirects you have posted it looks like you are simply injecting /products/jewelry at the start of the URL-path. This does not need 4 separate rules, providing you are wanting to redirect all URLs that following this particular format.
Try the following instead:
RewriteEngine On
# Inject (redirect) "/product/jewelry" at the start of the URL-path
RewriteRule ^buy-online-alhabib-rings\d-sku-\d+$ /products/jewelry/$0 [R=301,L]
# Internal rewrite
RewriteRule ^buy-online-(.*) displayitems.php?url=$1 [L]
The $0 backreference in the first rule contains the entire URL-path that is matched by the RewriteRule pattern.
Note that I also removed ./ from the start of the substitution string in the last rule. This is unnecessary here.

How add path to url if the url does not contain substring

I need to redirect all requests from
https://example.com/* to https://example.com/test/* if the URL does not contain the test substring.
So far I have these rewrite rules
RewriteBase /
RewriteCond %{THE_REQUEST} !^/test
RewriteRule ^/?$ /test/$1 [R=301,L] # if the url does not contain test, redirect to url with test
RewriteCond %{THE_REQUEST}% test
RewriteRule ^test?(.*)$ /$1 [L] # mask the fact that the url is not https://example.com/ and instead is https://example.com/test but apache serve the website like if it was on root
If I access https://example.com it redirects to https://example.com/test but gives infinite loop because of the second rule.
How can I combine it, so request to https://example.com/test* do not get redirected but those request at https://example.com/* do without having to change www root directory and so it will work for all URLs.
UPDATE:
The test should be in url (for user experience), but the apache should route like if it was not in url and instead the request came to root url, so application routing is preserved internally without having to change the app itself.
Ah, Ok. However, you should be linking to the /test URLs within your app (so you do still need to "change the app", despite your last comment), otherwise /test isn't actually in the URLs that users and search engines see on the page (they will be redirected) and your users will experience an external redirect every time they click one of your links (bad for SEO and user experience).
The "redirect" implemented in .htaccess to prefix "old" URLs with /test is just for SEO - as with any "old" to "new" URL change. (It should not be required for your app to function - with /test in the URL-path - since your internal URLs should already include /test.)
Try it like this instead:
RewriteEngine On
# Insert "/test" at the start of the URL-path if absent in "direct" requests
RewriteRule %{ENV:REDIRECT_STATUS} ^$
RewriteRule !^test/ /test/$1 [R=301,L]
# Rewrite "/test" URLs back to root
RewriteRule ^test(?:$|/(.*)) /$1 [L]
The REDIRECT_STATUS environment variable is used to prevent a redirect loop. This is empty on the initial request from the client and set to the HTTP response status after the rewrite (below).
Test first with 302 (temporary) redirects and only change to 301 (permanent) when you are sure this is working as intended.
You will need to clear your browser cache before testing.

External and internal redirection using htaccess

I want to convert http://mywebsite.com/folder/file.html to http://mywebsite.com/file.
I am not using regular expression as first I am only concerned about this one URL only.
I tried this-
Redirect 301 /folder/file.html http://mywebsite.com/file
Using this I am able to externally redirect this URL to the desired one but since the URL doesn't exist so I am getting 404.
Now, in order to internally redirect the new URL to the old one, I am using below command but it doesn't seem to work-
RewriteRule http://mywebsite.com/file http://mywebsite.com/folder/file.html [L]
Use only mod_rewrite directives and use THE_REQUEST variable for external redirection.
# turn on rewrite engine
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+/folder/file\.html[\s?] [NC]
RewriteRule ^ /file [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^file/?$ /folder/file.html [L,NC]

.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.
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]
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.

Map all requests from a subdomain to a different url

I have a server that responds to www.server.com/api
and I would like to have that same server respond to api.server.com
currently, I set up api.server.com to be an alias of www.server.com in my vhosts file, but I need to write a rule to add the /api on all requests with the api.server.com domain.
I've tried this:
RewriteEngine on
RewriteCond %{SERVER_NAME} api.server.com
RewriteRule ^(.*)$ /api/$1 [L,R=301]
but either way I play around with this, I get 500 because of a loop condition.
According to your description you do not want to implement an external redirection. Instead try that:
RewriteEngine on
RewriteCond %{SERVER_NAME} ^api\.server\.com$
RewriteCond %{REQUEST_URI} !^/api
RewriteRule ^(.*)$ /api/$1 [L]
It will have all requests to https://api.server.com/... get processed by /api/... without rewriting the visible URL in the browser. Also it will not result in an endless rewriting loop for direct or internal requests to /api/....
If you operate a current version of the apache http server you can replace the old L flag with the END flag which will prevent an additional rewriting iteration, thus save time and performance.