Issue using .htaccess on a linux server - .htaccess

So I have a site where once there was a wiki structure in php
Now I have a completely clean site now started up from scratch (so no hidden .htaccess files etc. or other installed components) but need to redirect incoming url requests for the old site to a new html structure
I imagined I could do it using an .htaccess file
I have tried a variation of syntax in the .htaccess file for example:
<IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RedirectPermanent 3dprintfo.com/doku.php?id=3dproject2 http://3dprintfo.com/3dproject2.html
<IfModule mod_rewrite.c/>
<IfModule>
And for example:
RedirectPermanent 301 3dprintfo.com/doku.php?id=3dproject2 http://3dprintfo.com/3dproject2.html
RedirectPermanent http://3dprintfo.com/doku.php?id=3dproject2 http://3dprintfo.com/3dproject2.html
However nothing works I keep getting:
Not Found
The requested URL was not found on this server.
Additionally, a 404 Not Found error was encountered while trying to use an ErrorDocument to handle the request.
I have tried with and without http/301 etc.
Can anyone try to show me an example with multiple url redirects that I can try to implement using my above url names...

This would be an example of a working redirection, as far as I understand your question:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?3dprintfo\.com$
RewriteCond %{QUERY_STRING} ^id=3dproject2$
RewriteRule ^/?doku\.php$ /3dproject2.html [R=301,QSD,L]
Of course it is possible to implement more general rules, so that you won't need a catalog of rules for each and every "old page". But that obviously depends on your link structure used previously.
In general I would suggest that you should implement such rule in the actual http server's host configuration and not in a distributed configuration file (".htaccess"). But above rule is written such that it works in both locations.
The first RewriteCond might not be required, depends on what else is configured inside your http server.
UPDATE:
For what you ask in your first comment to this question I'd suggest something along these lines:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?3dprintfo\.com$
RewriteCond %{QUERY_STRING} ^id=([^&]+)(?:&|$)
RewriteCond %{DOCUMENT_ROOT}/%1.html -f
RewriteRule ^/?doku\.php$ /%1.html [R=301,QSD,L]
This variant additionally checks if a file named for example 3dproject2.html actually exists before rewriting to it.

Related

Trouble with redirects with multiple directories

I have a domain with two versions and I need to redirect 1 of the versions
test.example.ca
test.example.ca/en
test.example.ca/fr
I need the first domain test.example.com to redirect to test.example.ca/en anytime someone hits it. but i don't want test.example.ca/fr to redirect to test.example.com/en/fr
this is what I've been trying with no success.
RewriteCond %{HTTP_HOST} =test.example.ca
RewriteCond %{HTTP_HOST} !=test.example.ca/fr
RewriteRule ^(.*)$ https://%{HTTP_HOST}/en/$1 [R=301,L]
I understand the question such that you simply to not want requests to https://test.example.com/fr... to get redirected. So you want an exception.
I'd say this roughly is what you are looking for:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^test\.example\.com$
RewriteCond %{REQUEST_URI} !^/fr
RewriteRule ^ https://test.example.ca/en%{REQUEST_URI} [R=301,L]
Chances are that your question was wrong in a few details, to me it reads as if you were not really precise with your host names. But above should be the correct answer to what you actually asked.
You should implement such rules in the http server's host configuration. If you do not have access to that you can also use a distributed configuration file (".htaccess"). That file should be located in the DOCUMENT_ROOT folder defined for your http host. And you need to make sure that the interpretation of such files is enabled at all (see the documentation for the AllowOverride directive for that).
It is a good idea to start out using a R=302 temporary redirection first. And to only change that to a R=301 permanent redirection once everything works as desired. That prevents nasty caching issues on the client side.

I need to .htaccess redirect my subfolder to the root folder, except two folders

The subfolder i want to redirect to the root folder is www.example.com/subfolder
So I need to redirect everything in that subfolder to the url adress https://www.example.com except for these two folders (which are placed in the subfolder): www.example.com/subfolder/folder1 and www.example.com/subfolder/folder2
I spent like 4 hours trying to find the exact code but I could not solve that. Nothing worked for me.
I've tried many codes, but nothing worked for me. For example:
RewriteEngine On
RewriteCond %{REQUEST_URI}!^/subfolder/folder1/
RewriteCond %{REQUEST_URI}!^/subfolder/folder2/
RewriteRule ^(.*)$ /$1 [R=301,L]
and
RewriteEngine on
Options +FollowSymlinks
RewriteCond %{REQUEST_URI}!^/subfolder/folder1/
RewriteCond %{REQUEST_URI}!^/subfolder/folder2/
#RewriteRule (.*) http://www.example.com/subfolder/ [R=301,QSA,L]
RewriteRule (.*) https://www.example.com [R]
Could someone help me with that?
I'd prefer to place the .htaccess file in the subfolder.
Thank you so much.
I'd say that your first attempt looks pretty good. Just make a slight modification since you say you want to redirect to the root path, not something inside the root path:
RewriteEngine On
RewriteCond %{REQUEST_URI}!^/subfolder/folder1/
RewriteCond %{REQUEST_URI}!^/subfolder/folder2/
RewriteRule ^(.*)$ / [R=301,QSD,L]
Since you want to use a distributed configuration file (as opposed to the preferred central configuration for the http host) that rule is meant to be used inside a ".htaccess" file inside the "subfolder".
It is a good idea to start out using a R=302 temporary redirection and to only change that to a R=301 permanent redirection once everything works as intended. Also you need to make sure that you always test using a fresh anonymous browser window to prevent client side caching effects.
And you also need to make sure that such distributed configuration files are considered at all by the http server for that location (see the documentation of the AllowOverride directive).

Rewrite a complex link .htaccess

My old website have product link like this
domain-name/index.php/product-category/detail/603/product-name.html
my new Wordpress website have product link like this
domain-name/product-category/product-name
i config in .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^product-category/detail/([0-9]+)/([a-zA-Z-]*).html /product-category/$2 [QSA,L]
in permalink i config:
/%category%/%postname%
The new site works very correctly, but the old link is not working. Old link always redirect to error 404 absolute, i want to keep old link on google auto redirect to new link. Does anyone have a solution for this please, thank u very much.
This should be your rule modified to actually match the requested URL you show:
RewriteEngine On
RewriteRule ^/?index\.php/product-category/detail/\d+/([\w-]+)\.html /product-category/$2 [QSA,END]
But more likely you want the more flexible variant:
RewriteEngine On
RewriteRule ^/?index\.php/([\w-]+)/detail/\d+/([\w-]+)\.html /$1/$2 [QSA,END]
You should prefer to implement such rules in the actual http server's host configuration. If you have no access to that you can use a distributed configuration file (often called ".htaccess"), but that comes with some disadvantages.
I could imagine that it also makes sense to send an external redirection to clients still requesting those old URLs:
RewriteEngine On
RewriteRule ^/?index\.php/([\w-]+)/detail/\d+/([\w-]+)\.html /$1/$2 [QSA,R=301,END]
For that it might make sense to start out with a R=302 temporary redirection and to change that to a R=301 permanent redirection once you checked that everything works as expected. That prevents annoying caching issues...

Redirect any url which have specific keyword

Trying to redirect url to specific url which includes /for-your-practice/.
if we get to know that url contains for-your-practice just redirect it to the url i want. which would be https://www.example.com/first/second/order/shop/product-category/for-your-practice/
Tried solutions so far:
First:
Redirect /for-your-practice/
https://www.example.com/first/second/order/shop/product-category/for-your-practice/
Second:
Redirect 302 https://www.example.com/first/second/order/shop/for-your-practice/ https://www.example.com/first/second/order/shop/product-category/for-your-practice/
Third:
#RewriteCond %{REQUEST_URI} www.example.com/first/second/order/shop/product-category/for-your-practice [NC]
#RewriteRule ^(.*)$ /for-your-practice [R,L]
Fourth:
RewriteCond %{QUERY_STRING} /for-your-practice/
RewriteRule .*$ http://www.example.com/first/second/order/shop/product-category/for-your-practice [L,R=301]
unfortunately none of them worked, what's wrong i'm doing here ?
.htaccess
Easiest probably is to use apache's rewriting module:
RewriteEngine on
RewriteCond %{REQUEST_URI} /for-your-practice/
RewriteRule ^ https://www.example.com/first/second/order/shop/product-category/for-your-practice/ [R=301]
For this some preconditions have to apply:
the interpretation of .htaccess tyle files has to be enabled
the rewriting module has to be loaded
the .htaccess style file has to be located in your DocumentRoot
the .htaccess style file has to be readable by the http server process
Reason for the use of an additional RewriteCond is that preceding slashes in URLs can raise issues with RewriteRules in .htaccess style files. IN general you should always prefer to place such rules in the http servers host configuration instead of using .htaccess style files. Those files are notoriously error prone, hard to debug and they really slow down the server, often without need. Those files are only provided as a last means for situations where one can not use the host configuration (read: really cheap hosting providers) or for applications that need to write their own rewriting rules (which obviously is a security nightmare).

htaccess rewrite query string nothing works

THE PROBLEM
After looking at 50+ StackOverflow posts and trying many permutations of my htaccess file, it does nothing still.
WHAT I HAVE TRIED
Using this website to generate my htaccess file: http://www.generateit.net/mod-rewrite/
Setting AllowOverride All in my httpd.conf file and restarting Apache.
MY CURRENT HTACCESS FILE
Lives in the root directory.
RewriteEngine On
RewriteBase /
RewriteRule ^find-a-local-doctor/([^/]*)/([^/]*)$ /find-a-local-doctor/?state=$1&city=$2 [L]
WHAT I WANT TO ACCOMPLISH
Change this URL:
http://www.md1network.com/find-a-local-doctor/?state=FL&city=Tampa
To this:
http://www.md1network.com/find-a-local-doctor/FL/Tampa
ADDITIONALLY
Since the actual file doing the work is: http://www.md1network.com/find-a-local-doctor/index.php, I need to be able to parse the query string with PHP. Hopefully, I will still be able to do this to get the state and city.
Please help.
Thanks.
Your existing rule looks alright but you will need an additional external redirection rule for reverse. Put this rule before your existing rule (just below RewriteBase /).
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(find-a-local-doctor)/(?:index\.php)?\?state=([^&]+)&city=([^&\s]+) [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]

Resources